Virtual Environments#
python -m venv .venv
# Activate the environment
source .venv/bin/activate
# Deactivate the environment
deactivate
Python Syntax (basic)#
## One line comment
'''
multi
line
comment
'''
## Python uses indentation to define code blocks.
## Commonly 4 spaces or tab
if age > 1:
print('Hello there!')
## Line Continuation
total = a+b+c+d+e+f+\
g+h+d
## Multiple statements
x=5;y=10;z=x+y
## Semantics in Python
total = 100 ## total is an integer
kind = "Jedi" ## kind is a string
## Type inference
var = 10
print(type(var))
var = "Moon"
print(type(var))
Variables in Python#
total=100 # integer
size=1.6 # float
name="Plagueis" # string
alive=True # boolean
Naming Conventions#
## Variables must start with a letter or an '_'.
## Can contain letters, numbers and underscores.
## Variable names are case sensitive
## Type of a variable is determined at runtime
Simple Data Types in Python#
## Data Types are the classification of data that tell
## the interpreter how the programmer wants to use the data.
## Data types determine the type of operations that can be
## performed on the data and the amount of memory needed to store the data
## Integers
i=0
## Strings
msg="Hello World!"
## Floating point
distance=1.25
## Boolean
acitve=True
Advanced Data Types in Python#
## Lists
ages = [10,11,12,13]
## Tuples are immutable (Cannot be changed)
cars = ('Tesla', 'BMW', 'Toyota')
## Sets
student_id = {100,101,102,103,104}
## Dictionaries
country_capitals = {
"Slovenia": "Ljubljana",
"Croatia": "Zagreb",
"Spain": "Madrid
}
Python Operators#
## Addition
## Subtraction
## Multiplication
## Division
## Floor Division
## Modulus
## Exponentiation
## Equal to
## Not equal to
## Greater than
## Less than
## Greater than or equal to
## Less than or equal to
## AND
## OR
## NOT
Flow Control#
## if
if age>=18:
print('You are allowed')
## else
if age>=18:
print('You are allowed')
else:
print('You shall not pass')
## elif - if but with multiple conditions
if age<13:
print('You are a child')
elif age<18:
print('You are a teenage')
else:
print('You are an adult')
## Nested conditions
## Just don't do it.
Loops#
## For Loop
for i in range(5):
print(i)
## While Loop
count=0
while count<5:
print(count)
count=count+1
## Break control statements
for i in range(5):
if i==2:
break
## Continue control statement
for i in range(10):
if i%2==0: # check if the number is even
continue
print(i)
## Pass control statement
for i in range(5):
if i==3:
pass # do nothing if i equals 3
Inbuilt Data Structures#