Click on a topic to expand and learn in detail.
Python is a high-level, interpreted programming language known for simplicity and readability.
# Hello World
print("Hello World")
# Variables
name = "Alice"
age = 25
pi = 3.14
# Data types: int, float, str, bool, list, tuple, dict, set
# Arithmetic
sum = 5 + 3
# Comparison
print(5 > 3) # True
# Logical
print(True and False) # False
age = 18
if age >= 18:
print("Adult")
elif age == 17:
print("Almost Adult")
else:
print("Minor")
# For loop
for i in range(5):
print(i)
# While loop
i = 0
while i < 5:
print(i)
i += 1
def greet(name):
return "Hello " + name
print(greet("Alice"))
# Lambda function
add = lambda a,b: a+b
print(add(5,3))
fruits = ["Apple", "Banana", "Mango"]
fruits.append("Orange")
print(fruits[0]) # Apple
tup = (1,2,3)
s = {1,2,3,3} # Set removes duplicates
person = {"name": "Alice", "age": 25}
print(person["name"])
person["city"] = "New York"
text = "Hello World"
print(text.upper())
print(text.lower())
print(text.replace("World", "Python"))
# Write to file
with open("file.txt", "w") as f:
f.write("Hello Python")
# Read file
with open("file.txt", "r") as f:
print(f.read())
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello " + self.name)
p1 = Person("Alice", 25)
p1.greet()
# Import built-in module
import math
print(math.sqrt(16))
# Import custom module
# import mymodule