Operator | Usage |
---|---|
+ | Adds two numbers |
- | Subtracts two numbers |
* | Multiplies two numbers |
/ | Divides two numbers |
// | Returns the integer of a division |
% | Returns the rest of a division |
Example :
>>> 2+3
# The result will be 5
>>> 3*3
# The result will be 9
variable_name = value
Note:
- The variable name may only consist of upper or lower case letters, numbers and the underscore symbol _.
- The variable name cannot begin with a number.
- the Python language is case sensitive, which means that upper and lower case letters are not the same variable (the variable AGE is different from aGe, which is itself different from age).
There are 2 main conventions for naming a variable:
my_age
.myAge
.myAge = 19
We can interact with this variable by modifying its content as we see fit:
myAge = 19
# myAge is equal to 19
myAge = myAge + 4
# myAge is equal to 23
myAge = myAge * 2
# myAge is equal to 46
Type of data | Comment |
---|---|
Integers (int) | Number without comma : 3 |
Floating numbers (float) | Number with comma : 3.2442 |
String | Contains letters, phrases : Hello! |
For strings of characters, you have to pay attention to the types of quotes used :
mySentence = 'I'll learn Python'
# Incorrect syntax!
# To correct this, you have to escape the ':
mySentence = 'I\'ll learn Python'
# Displays the sentence well
mySentence = "I'll learn Python"
# I'll learn Python
mySentence = """I'll
learn
Python"""
# The three quotation marks prevent the characters from being dropped, and allow you to go back to the line without using \n
When it comes to incrementing variables, there are several ways to do this:
# Method 1: Add the value to the variable
myVar = 1
myVar = myVar + 1
# Method 2: Directly increment the variable
myVar = 1
myVar += 1
>>> a = 5
>>> b = 32
>>> a,b = b,a # swapping
>>> a
32
>>> b
5
>>>
>>> x = y = 3
>>> x
3
>>> y
3
>>>
Syntax of a function :
function_name(parameter_1,parameter_2,…,parameter_n)
a=3
type(a)
# returns to class 'int'.
>>> a = 3
>>> print(a)
>>> a = a + 3
>>> b = a - 2
>>> print("a =", a, "et b =", b)