Variable Assignment
Here we will learn how to assign data to variable
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
listandstr - avoid using the single characters
l(lowercase letter el),O(uppercase letter oh) andI(uppercase letter eye) as they can be confused with1and0
my_dogs = 2
my_dogs
my_dogs = ['Sammy', 'Frankie']
my_dogs
p = 10
p
l = 5
l
Here we assigned the integer object 5 to the variable name a.
Let's assign a to something else:
a = 10
a
You can now use a in place of the number 10:
a + a
a = a + 10
a
There's actually a shortcut for this. Python lets you add, subtract, multiply and divide numbers with reassignment using +=, -=, *=, and /=.
a += 10
a
a *= 2
a
type(a)
a = (1,2)
type(a)
my_income = 100
tax_rate = 0.1
my_taxes = my_income * tax_rate
my_taxes
Great! You should now understand the basics of variable assignment and reassignment in Python.
Up next, we'll learn about strings!