Learning Python
Python, an open-source language, requires a single installation. It's cross-platform, functioning on Windows, Mac, and Linux, and encourages community contributions.
Python Installation
- Start off by going to this website -> Downloads Python
- Click on the downloads tab and choose the operating system and Python version.
- Double-click the executable and follow the steps to install python.
- To Verify the installation ~\AppData\Local\Programs\Python\Launcher and double click "py.exe". If you receive no error you are good to go.
Now that we have installed Python, letโs go ahead with this tutorial with programming in Python
- Single line and Multi-line comment
# This is a single-line comment
"""
This is a
multi-line comment
"""
- Variable and Data Types with Print()
int_var = 99 # Integer
float_var = 7.12 # Float
str_var = "this is a string variable" # String
list_var = [7, 8, 9] # List
dict_var = {"State": "QLD"} # Dictionary
print(int_var)
print(float_var)
print(str_var)
print(list_var)
print(dict_var)
- Arithmetic Operators
a = 100
b = 30
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
modulus = a % b
exponentiation = a ** b
floor_division = a // b
- Comparison Operator
x = 15
y = 10
equals = x == y
not_equals = x != y
greater_than = x > y
less_than = x < y
greater_equal = x >= y
less_equal = x <= y
- Logical Operators
p = True
q = False
logical_and = p and q
logical_or = p or q
logical_not = not p
- Assignment Operators
x = 5
x += 2 # Equivalent to x = x + 2
x -= 1 # Equivalent to x = x - 1
- Bitwise Operator
a = 5 # 101 in binary
b = 3 # 011 in binary
bitwise_and = a & b # 001 (1 in decimal)
bitwise_or = a | b # 111 (7 in decimal)
bitwise_xor = a ^ b # 110 (6 in decimal)
bitwise_not = ~a # -6
bitwise_left_shift = a << 2 # 20
bitwise_right_shift = a >> 1 # 2
- Membership Operators
my_list = [1, 2, 3]
is_in = 2 in my_list
is_not_in = 4 not in my_list
- Identity Operators
x = [1, 2, 3]
y = x
is_same_object = x is y
is_not_same_object = x is not None
- Conditional Statements (if , else if and else)
if condition:
# Code to execute if condition is True
elif another_condition:
# Code to execute if another_condition is True
else:
# Code to execute if no conditions are True
- Loop Statements (for ,while and others)
for item in iterable:
# Code to execute for each item
while condition:
# Code to execute while condition is True
- FOR Statement Example. The for loop iterates through a list of fruits, printing each fruit's name.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
- WHILE Statement Example. The while loop counts and prints numbers until it reaches 5.
count = 0
while count < 10:
print(count)
count += 1
- Break Statement Example. The break statement is used to exit a loop prematurely when the number 5 is encountered in the first 3 numbers.
for number in range(1, 10):
if number == 5:
break
print(number)
- Continue Statement Example. The continue statement skips the number 3 in the loop and continues to the next iteration.
for number in range(1, 6):
if number == 3:
continue
print(number)
- Pass Statement Example. The pass statement is a placeholder used when you want to do nothing within a loop but need the loop's structure to be valid.
for i in range(1, 4):
pass # Placeholder for future code