Python Basics
Python Basics
Introduction to Python
History of Python
Python was created by Guido van Rossum in the late 1980s and released in 1991. It was
designed to emphasize code readability and simplicity. The name 'Python' comes from the
British comedy group Monty Python.
Why Python?
Installing Python
Output:
Hello, Python!
Comments in Python
# This is a single-line comment
"""
This is a multi-line comment
"""
String Operations
name = "Python"
print(name.upper()) # Convert to uppercase
print(name.lower()) # Convert to lowercase
print(name[0]) # Access first character
print(name[::-1]) # Reverse the string
Output:
PYTHON
python
P
nohtyP
Simple Input & Output
user_input = input("Enter your name: ")
print("Hello,", user_input)
Example Input:
Alice
Example Output:
Hello, Alice
Operators in Python
a = 10
b=5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Modulus:", a % b)
print("Exponentiation:", a ** b)
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Modulus: 0
Exponentiation: 100000
if True:
print("This is correctly indented")
Output:
Conditional Statements
x = int(input("Enter a number: "))
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is 5")
else:
print("x is less than 5")
Loops in Python
while Loop
i=1
while i <= 5:
print(i)
i += 1
for Loop
for i in range(5):
print(i)
def greet(name):
print("Hello,", name)
greet("Alice")
Output:
Hello, Alice
result = add(5, 3)
print("Sum:", result)
Output:
Sum: 8
greet()
greet("John")
Output:
Hello, Guest
Hello, John
Lambda Functions in Python
Lambda functions are anonymous, single-line functions.
square = lambda x: x ** 2
print(square(5))
Output:
25
Output:
10