Python Variables and Experissions

Variables are names of places in memory

Since it is variable, then the values changes

Reserve a place in memory named x and store the value 10 in it.

In [1]:
x = 10

Variables should be defined before using them

In [2]:
print(y) # name error because y is not defined 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-c95a47552421> in <module>
----> 1 print(y) # name error because y is not defined

NameError: name 'y' is not defined
In [3]:
y = 20 
print(y) # now it is OK :) 
20

Variable names should meaningful

names should be nouns or noun phrases

In [4]:
car_speed = 10 # good name 
car = 'BMW' # good name 
name = 'Ahmed' # good name 
a = 10 # bad name

Naming variables in Python

  1. shoud start with _ or letter
  2. variables are case sentsetive
  3. No special charachters
In [5]:
car1 = 'BMW' # good name 
In [6]:
1car = 'BMW' # bad name (syntax error )
  File "<ipython-input-6-9e532e4307f9>", line 1
    1car = 'BMW' # bad name (syntax error )
       ^
SyntaxError: invalid syntax
In [7]:
car$speed = 100 # bad name (syntax error)
  File "<ipython-input-7-72f9aee6a4f8>", line 1
    car$speed = 100 # bad name (syntax error)
       ^
SyntaxError: invalid syntax

Statements

In [8]:
x = 10  # assignment
In [9]:
y = x + 3  # expression and assignment

Experssions

In [10]:
print(2 ** 3)  # power: 2*2*2
8
In [11]:
print(divmod(23, 5))  # (4, 3)
(4, 3)
In [12]:
print(23 // 5)  # 4
4
In [13]:
print(23 % 5)  # 3
3

Operator Precedence Rules

  • Parentheses are always respected
  • Exponentiation (raise to a power)
  • Multiplication, Division, and Remainder
  • Addition and Subtraction
  • Left to right
In [14]:
x = 0.6
3.9 *  x  * ( 1  -  x )
Out[14]:
0.9359999999999999

Python Types int, float, str

In [15]:
x = 10
print(type(x))  # <class 'int'>
<class 'int'>
In [16]:
x = 10.1
print(type(x))  # <class 'float'>
<class 'float'>
In [17]:
x = 'abc'
print(type(x))  # <class 'str'>
<class 'str'>

int / int = always result is float number

In [18]:
print(10 / 3)  # float result
print(10 / 2)  # float result
3.3333333333333335
5.0

String concat

In [19]:
print('abc' + '123')  # concat
abc123
In [20]:
print('abc' - 'qwerty') # error
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-84222a2e9099> in <module>
----> 1 print('abc' - 'qwerty') # error

TypeError: unsupported operand type(s) for -: 'str' and 'str'

Type conversions

In [21]:
# int to str 
print(str(123))
123
In [22]:
# flaot to str 
print(str(12.3))
12.3
In [23]:
# str to int 
print(int('133'))
133
In [24]:
print(int('133abc')) # error 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-4703d586a22e> in <module>
----> 1 print(int('133abc')) # error

ValueError: invalid literal for int() with base 10: '133abc'
In [25]:
print(int('13.3')) # error 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-6f875ff57188> in <module>
----> 1 print(int('13.3')) # error

ValueError: invalid literal for int() with base 10: '13.3'
In [26]:
# string to flaot 
print(float('13.3'))
13.3
In [27]:
print(float('13.3.1')) # error 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-00ba8a3eea00> in <module>
----> 1 print(float('13.3.1')) # error

ValueError: could not convert string to float: '13.3.1'