Home / Tutorials Home
Python Foundations
Python supports various data types:
# Variables and data types
name = "Alice" # String
age = 30 # Integer
height = 5.6 # Float
is_student = True # Boolean
Performing operations in Python:
# Basic operations
result = 10 + 5 # Addition
difference = 20 - 8 # Subtraction
product = 6 * 4 # Multiplication
quotient = 15 / 3 # Division
remainder = 10 % 3 # Modulo (remainder)
Using if-else statements:
# Control structures
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
Using for and while loops:
# Loops
for i in range(5):
print(i) # Prints numbers 0 to 4
counter = 0
while counter < 5:
print(counter)
counter += 1 # Prints numbers 0 to 4
Creating and using functions:
# Functions
def greet(name):
print("Hello,", name)
greet("Bob") # Calls the function with an argument
Working with collections:
# Lists and dictionaries
my_list = [1, 2, 3, 4] # List
print(my_list[0]) # Accessing elements
my_dict = {"name": "Alice", "age": 30} # Dictionary
print(my_dict["name"]) # Accessing values by keys
Taking user input and displaying output:
# Input/Output
user_input = input("Enter your name: ")
print("Hello,", user_input)
Absolutely, let’s delve deeper into more advanced Python topics:
Functions encapsulate reusable code:
# Functions
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3) # Calling the function
print(result) # Output: 8
Tuples and sets in Python:
# Tuples and sets
my_tuple = (1, 2, 3) # Tuple
print(my_tuple[0]) # Accessing elements
my_set = {1, 2, 3, 3} # Set
print(my_set) # Output: {1, 2, 3} (sets don't allow duplicates)
Creating classes and objects in Python:
# Object-Oriented Programming (OOP)
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} says Woof!")
# Creating an instance (object) of the class
my_dog = Dog("Buddy", 3)
my_dog.bark() # Output: Buddy says Woof!
Handling exceptions in Python:
# Exceptions
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
Reading and writing to files:
# File I/O
with open("example.txt", "w") as file:
file.write("Hello, this is a sample text.") # Writing to a file
with open("example.txt", "r") as file:
content = file.read() # Reading from a file
print(content) # Output: Hello, this is a sample text.