A variable is simply a name given to a memory location that holds a value.
Since Python is a dynamically typed language, a variable is created by simply assigning a value to it. You do not declare variables as in other languages.
age = 21
message = 'you are of drinking age.'
The value assigned to a variable defines its type. So age
is an int
and message
a str
.
age = 21
message = 'you are of drinking age.'
print('If you are', age, message)
If you are 21 you are of drinking age.
You can always find out the type of a variable or value by using the type()
function.
type(var)
type(value)
print( type(age) )
print( type(1) )
print( type( message) )
print( type("hi") )
<class 'int'> <class 'int'> <class 'str'> <class 'str'>
Give each variable a name that identifies the value it holds. That makes your code more readable and easier to follow.
If a variable name is made up of multiple words here are your options:
my_long_variable_name
myLongVariableName
Option 2 is referred to as lowercase camel notation.
If you attempt to use a variable that has not been defined, then you will get an error.
print(x)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /var/folders/_5/pbybv9c90j77vqh9wby2v8140000gq/T/ipykernel_94667/1353120783.py in <module> ----> 1 print(x) NameError: name 'x' is not defined
Every language, Python included, uses special reserved names called keywords that cannot be used as variable names. If you use such a keyword you will get an error.
print(if)
File "/var/folders/_5/pbybv9c90j77vqh9wby2v8140000gq/T/ipykernel_94667/670857461.py", line 1 print(if) ^ SyntaxError: invalid syntax
An expression consists of values, variables, and operators.
3 + 3
n + 2
Each expression will evaluate to a value.
A statement is any code that has some effect, e.g. creating a variable or printing a value.
n = 3
print(n)
The division and remainder operators deserve a quick tour.
The /
produces real results, regardless of each value's type.
print(1 / 2)
print(1.0 / 2.0)
print(1.0 / 2)
0.5 0.5 0.5
The //
peforms a floor division, discarding any fractional part. It returns the immediate predecessor integer value.
Notice the last result which may be surprising to you. 3.0 // 2
. The result of the division 1.5
evaluates to 1.0
.
print(1 // 2)
print(3 // -2)
print(3.0 // 2)
0 -2 1.0
The %
returns the remainder of a floor division, but taking into account the type of the operands.
In order to do this keep in mind that D = d * q + r
, where:
D
is the dividendd
is the divisorq
is the result of the floor divisionr
is the remainderprint(1 % 2) # r = 1 since 1 // 2 = 0.5 => 0 thus 1 = 2 * 0 + 1
print(3 % 2) # r = 1 since 3 // 2 = 1.5 => 1 thus 3 = 2 * 1 + 1
print(3.0 % 2) # r = 1.0 since 3.0 // 2 = 1.5 => 1.0 thus 3.0 = 2 * 1.0 + 1.0
print(-3 % 6) # r = 3 since -3 // 6 = -0.5 => -1 thus -3 = 6 * (-1) + 3
print(3 % -6) # r = -3 since 3 // -6 = -0.5 => -1 thus 3 = -6 * (-1) + (-3)
print(6 % -4) # r = -2 since 6 // -4 = -1.5 => -2 thus 6 = -4 * (-2) + (-2)
print(-6 % 4) # r = 2 since -6 // 4 = -1.5 => -2 thus -6 = 4 * (-2) + 2
1 1 1.0 3 -3 -2 2
When a negative value is involved as in the last two examples above, the sign of the result is the same as the sign of the divisor.
The two operators +
and *
have special powers when dealing with strings.
The +
concatenates two strings together:
str1 + str2
while the *
performs repetion.
str * int_value
print('Hello' + 'there.')
print('Tra' + ' la' * 2)
Hellothere. Tra la la
Python uses the #
symbol for its comments. Place a comment on a line by itself (block-comment) or at the end of a statement (inline).
'''A docstring describes the module or function
and can span
many lines.
'''
# A block comment
x = 10
y = 20 # inline comment
Use the notebook in VSC to complete the exercises or an interactive Python session if you are reading the web page.
# Type your code here
# Type your code here
# Type your code here