You can name a variable anything as long as it obeys the following rules:
>>> # this wont work
>>> my variable = 'Hello'
>>> # good
>>> var = 'Hello'
_
) character.>>> # this wont work
>>> %$@variable = 'Hello'
>>> # good
>>> my_var = 'Hello'
>>> # good
>>> my_var_2 = 'Hello'
>>> # this wont work
>>> 23_var = 'hello'
>>> # this wont work
>>> if = 'hello'
>>> # this is bad
>>> print = 'world'
Data Type | Examples |
---|---|
Bool | True , False |
Integers | -2, -1, 0, 1, 2, 3, 4, 5 |
Floating-point numbers | -1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25 |
Complex numbers | 2+1j , 3-5j |
Strings | 'a', 'aa', 'aaa', 'Hello!', '11 cats' |
type()
to verify the datatype of the value assigned to a variable.
From Highest to Lowest precedence:
Operators | Operation | Example |
---|---|---|
** | Exponent | 2 ** 3 = 8 |
% | Modulus/Remainder | 22 % 8 = 6 |
// | Floor(Integer) division | 22 // 8 = 2 |
/ | Float(True) Division | 22 / 8 = 2.75 |
* | Multiplication | 3 * 3 = 9 |
- | Subtraction | 5 - 2 = 3 |
+ | Addition | 2 + 2 = 4 |
>>, << | shifting | 16 >> 2 |
&,^, | | bitwise-logical operator | 1^0 |
>,>=,== | Comparison operator | 2!=3 |
not, and, or | Logical operator | (4 < 5) and (5 < 6) |
=,+=,*= | assignment symbol | a +=1 |
Examples of expressions:
>>> 2 + 3 * 6
# 20
>>> (2 + 3) * 6
# 30
>>> 2 ** 8
#256
>>> 10 / 2
#5.0
>>> -11 / 2
#-5.5
>>> 23 // 7
# 3
>>> -5.0 // 2
# -3.0
>>> 23 % 7
# 2
>>> (5 - 1) * ((7 + 1) / (3 - 1))
# 16.0
Operator | Equivalent |
---|---|
var += 1 |
var = var + 1 |
var -= 1 |
var = var - 1 |
var *= 1 |
var = var * 1 |
var /= 1 |
var = var / 1 |
var %= 1 |
var = var % 1 |
var **= 2 |
var = var**2 |
Examples:
>>> greeting = 'Hello'
>>> greeting += ' world!'
>>> greeting
# 'Hello world!'
>>> number = 1
>>> number += 1
>>> number
# 2
my_string = 'characters'
my_Boolean = True # True/False
my_integer = 5
my_floating_point = 26.2
my_complex = 2+1j # Note that 1 can not be omitted
# The above code is equivalent to
my_string, my_Boolean, my_integer, my_floating_point, my_complex = 'characters', True, 5, 26.2, 2+1j
String concatenation:
>>> 'Alice' + 'Bob'
# 'AliceBob'
String Replication:
>>> 'Alice' * 5
# 'AliceAliceAliceAliceAlice'
Inline comment:
# This is a comment
Multiline comment:
# This is a
# multiline comment
Code with a comment:
a = 1 # initialization
Function docstring or multiline comments can also be used:
"""
This is a function docstring
You can also use it as multiline string
"""
print()
FunctionThe print()
function writes the value of the argument(s) it is given. It handles multiple arguments, floating point-quantities, and strings. Strings are printed without quotes, and a space is inserted between items, so you can format things nicely:
>>> print('Hello world!')
# Hello world!
>>> a = 1
>>> print('Hello world!', a)
# Hello world! 1
>>> print('Hello,\
World') # It is possible for statements to span more than one line using `\`
# Hello, World
>>> print("Welcome\nto\nthe course")
#Welcome
#to
#the course
The keyword argument end
can be used to avoid the newline after the output, or end the output with a different string:
print("Welcome", end='-')
print("to the course")
...
# Welcome-to the course
The keyword sep
specify how to separate the objects, if there is more than one, the default value is a white space:
print('cats', 'dogs', 'mice', sep=',')
# cats,dogs,mice
input()
FunctionThis function takes the input from the user and converts it into a string:
>>> print('What is your name?') # ask for their name
>>> my_name = input()
>>> print('Hi' + my_name)
# What is your name?
# Martha
# Hi, Martha
input()
can also set a default message without using print()
:
>>> my_name = input('What is your name? ') # default message
>>> print('Hi' + my_name)
# What is your name? Martha
# Hi, Martha
len()
FunctionEvaluates to the integer value of the number of characters in a string, list, dictionary, etc.:
>>> len('hello')
# 5
bool()
, int()
, float()
, complex()
and str()
FunctionsThese functions allow you to change the type of variable. For example, you can transform from an integer
or float
to a string
:
>>> str(29)
# '29'
>>> str(-3.14)
# '-3.14'
Or from a string
to an integer
or float
:
>>> int('11')
# 11
>>> float('3.14')
# 3.14