Python Programming
Python is an interpreted, high-level, dynamic language.
Variables and Data Types
Python supports various data types including integers, floats, strings, and booleans.
Boolean
y = Truez = FalseNumericals
# Integerx = 5
# Floaty = 3.14
# Complexz = 3 + 4j # j is used instead of iString
name = "Alice"Strings can be sliced.
text = "Hello World"
# Basic slicing: text[start:end]print(text[0:5]) # "Hello" (index 0 to 4)print(text[6:]) # "World" (index 6 to end)print(text[:5]) # "Hello" (start to index 4)
# Using step: text[start:end:step]print(text[::2]) # "HloWrd" (every 2nd character)print(text[::-1]) # "dlroW olleH" (reverse string)Operator precedence
| Operator | Description |
|---|---|
** | Exponentiation |
~, +, - | Complement, unary plus, and minus |
*, /, %, // | Multiply; divide, modulo, floor division |
+, - | Addition and subtraction |
>>, << | Right and left bitwise shift |
& | Bitwise AND |
^, | | Bitwise XOR and OR |
<=, <, >, >= | Comparison operators |
==, != | Equality operators |
=, %=, /=, //=, -=, +=, *=, **= | Assignment operators |
is, is not | Identity operators |
in, not in | Membership operators |
not, or, and | Logical operators |
Control Structures
Python uses indentation to define blocks of code.
Selection
if x > 0: print("x is positive")elif x < -2: print("x is lesser than -2")else: print("x is between -2 and 0 (inclusive)")elif and else sections are optional.
match-case syntax can be used for pattern matching.
point = (0, 0)match point: case (0, 0): print("Origin") case (0, y): print(f"Y axis at {y}") case (x, 0): print(f"X axis at {x}") case (x, y): print(f"Point at {x}, {y}")Repetition
Executing a block of code multiple times.
# For loopfor i in range(5): print(i) # i ranges from 0 (inclusive) to 5 (exclusive)In for loop, the looping variable is automatically incremented.
# While loopcount = 0while count < 5: print(count) count += 1Subprogram (functions)
A reusable block of code that is used to perform a single action. Defined using
the def keyword, followed by its name and ().
def greet(name): return f"Hello, {name}!"
print(greet("Alice"))Lists
Lists are ordered collections of items. The items can be of any type. Items are indexed sequentially starting from 0.
fruits = ["apple", "banana", "cherry"]print(fruits[0]) # Output: apple
# Adding an item to the listfruits.append("date")
# Removing an item from the listfruits.remove("banana")
# Accessing the last itemprint(fruits[-1]) # Output: date
# Slicing the listprint(fruits[1:3]) # Output: ['cherry', 'date']
# Printing all the elements## 1st wayfor index in range(len(fruits)): print(fruits[index]) # index is a number between 0 and len(fruits)## 2nd wayfor fruit in fruits: print(fruit) # fruit is an item of the listTuples
Similar to lists but immutable (cannot be changed after creation). () are used
instead of [].
# Creating a tuplecoordinates = (4, 5)print(coordinates[0]) # Output: 4
# Tuple with different data typesmixed_tuple = (1, "hello", True)
# Cannot modify tuple# This will raise an error:# coordinates[0] = 10
# Creating tuple with one elementsingle_tuple = (1,) # Note the comma
# Multiple assignment using tuplex, y = coordinatesprint(x) # Output: 4print(y) # Output: 5Commonly used for returning multiple values from functions or when you want to ensure data cannot be modified.
Dictionary
Stores key-value pairs.
student = { "name": "Alice", "age": 21, "is_student": True}
print(student["name"]) # Output: Alice
# Adding a new key-value pairstudent["major"] = "Computer Science"
# Updating an existing valuestudent["age"] = 22
# Removing a key-value pairdel student["is_student"]
# Accessing all keysprint(student.keys()) # Output: dict_keys(['name', 'age', 'major'])
# Accessing all valuesprint(student.values()) # Output: dict_values(['Alice', 22, 'Computer Science'])There are a handful of built-in functions in Python.
Error Handling
Python uses try-except blocks for error handling.
try: # Code that might raise an exception x = 1 / 0except ZeroDivisionError: print("Cannot divide by zero!")except Exception as e: print(f"An error occurred: {e}")else: # Runs if no exception occurs print("Operation successful")finally: # Always runs print("Cleanup code")Common exceptions:
ValueError: Invalid valueTypeError: Invalid operation on typesFileNotFoundError: File doesn’t existIndexError: List index out of rangeKeyError: Dictionary key not found
Exceptions can be raised when required.
def divide(a, b): if b == 0: raise ValueError("Division by zero!") return a / b