Variables

You can name a variable anything as long as it obeys the following rules:

  1. It can be only one word.
>>> # this wont work
>>> my variable = 'Hello'

>>> # good
>>> var = 'Hello'
  1. It can use only letters (A-z), numbers(0-9), and the underscore (_) character.
>>> # this wont work
>>> %$@variable = 'Hello'

>>> # good
>>> my_var = 'Hello'

>>> # good
>>> my_var_2 = 'Hello'
  1. It can’t begin with a number.
>>> # this wont work
>>> 23_var = 'hello'
  1. Do not use keywords or built-in function as the variable name
>>> # this wont work
>>> if = 'hello'
>>> # this is bad
>>> print = 'world'

Data Types

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'

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Use the built-in function type() to verify the datatype of the value assigned to a variable.

Math Operators

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
You can use parenthesis to properly specify the order of operations in complex expressions.

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

Augmented Assignment Operators

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

Multiple assignment

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 and Replication

String concatenation:

>>> 'Alice' + 'Bob'
# 'AliceBob'

String Replication:

>>> 'Alice' * 5
# 'AliceAliceAliceAliceAlice'

Comments

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
"""

The print() Function

The 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

The escape sequence

>>> print("Welcome\nto\nthe course") #Welcome #to #the course

The end keyword

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 sep keyword

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

The input() Function

This 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

The len() Function

Evaluates to the integer value of the number of characters in a string, list, dictionary, etc.:

>>> len('hello')
# 5

The bool(), int(), float(), complex() and str() Functions

These 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