01-Variable Assignment
01-Variable Assignment
___
Content Copyright by Pierian Data
1 Variable Assignment
1.1 Rules for variable names
• names can not start with a number
• names can not contain spaces, use _ intead
• names can not contain any of these symbols:
:'",<>/?|\!@#%^&*~-+
• it’s considered best practice (PEP8) that names are lowercase with underscores
• avoid using Python built-in keywords like list and str
• avoid using the single characters l (lowercase letter el), O (uppercase letter oh) and I (upper-
case letter eye) as they can be confused with 1 and 0
[2]: my_dogs
[2]: 2
[4]: my_dogs
1
1.2.1 Pros and Cons of Dynamic Typing
Pros of Dynamic Typing
• very easy to work with
• faster development time
[6]: a
[6]: 5
Here we assigned the integer object 5 to the variable name a.Let’s assign a to something else:
[7]: a = 10
[8]: a
[8]: 10
[9]: 20
[11]: a
[11]: 20
There’s actually a shortcut for this. Python lets you add, subtract, multiply and divide numbers
with reassignment using +=, -=, *=, and /=.
[12]: a += 10
2
[13]: a
[13]: 30
[14]: a *= 2
[15]: a
[15]: 60
[16]: type(a)
[16]: int
[17]: a = (1,2)
[18]: type(a)
[18]: tuple
[20]: my_taxes
[20]: 10.0
Great! You should now understand the basics of variable assignment and reassignment in
Python.Up next, we’ll learn about strings!