0% found this document useful (0 votes)
10 views10 pages

Python Essentials

The document provides a comprehensive overview of Python essentials, covering topics such as basic syntax, control flow, functions, data structures, string manipulation, file handling, error handling, object-oriented programming, and modules. It includes practical examples and mini-projects to reinforce learning. Each section is designed to introduce key concepts and functionalities in Python programming.

Uploaded by

melamadithya11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Python Essentials

The document provides a comprehensive overview of Python essentials, covering topics such as basic syntax, control flow, functions, data structures, string manipulation, file handling, error handling, object-oriented programming, and modules. It includes practical examples and mini-projects to reinforce learning. Each section is designed to introduce key concepts and functionalities in Python programming.

Uploaded by

melamadithya11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Essentials

Page 1: Introduction & Basics

# Hello World
print("Hello, World!")

# Variables and Data Types


name = "Alice"
age = 25
is_student = True

# Input/Output
user_input = input("Enter something: ")
print("You entered:", user_input)
Python Essentials

Page 2: Control Flow

# If-else
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is 5 or less")

# Loops
for i in range(5):
print(i)

i = 0
while i < 5:
print(i)
i += 1
Python Essentials

Page 3: Functions

# Function with arguments and return


def add(a, b):
return a + b

print(add(3, 4))

# Lambda function
square = lambda x: x*x
print(square(5))
Python Essentials

Page 4: Data Structures

# List
fruits = ["apple", "banana", "cherry"]
print(fruits[0])

# Tuple
coordinates = (10, 20)

# Dictionary
student = {"name": "Bob", "age": 22}

# Set
unique_numbers = {1, 2, 3}

# List Comprehension
squares = [x*x for x in range(5)]
print(squares)
Python Essentials

Page 5: String Manipulation

# String Slicing
s = "Hello, World!"
print(s[0:5])

# String Methods
print(s.lower())
print(s.upper())

# String Formatting
name = "Alice"
print(f"Hello, {name}")
Python Essentials

Page 6: File Handling

# Writing to a file
with open("sample.txt", "w") as f:
f.write("Hello, file!")

# Reading from a file


with open("sample.txt", "r") as f:
content = f.read()
print(content)
Python Essentials

Page 7: Error Handling

try:
x = int(input("Enter a number: "))
print(10 / x)
except ValueError:
print("Invalid input!")
except ZeroDivisionError:
print("Cannot divide by zero!")
Python Essentials

Page 8: Object-Oriented Programming

class Person:
def __init__(self, name):
self.name = name

def greet(self):
print("Hello, my name is", self.name)

class Student(Person):
def __init__(self, name, student_id):
super().__init__(name)
self.student_id = student_id

s = Student("Alice", "S101")
s.greet()
Python Essentials

Page 9: Modules & Libraries

import math, random, datetime

print(math.sqrt(16))
print(random.randint(1, 10))
print(datetime.datetime.now())

# Custom module (e.g., my_module.py)


# def greet(name):
# return f"Hello, {name}"
Python Essentials

Page 10: Projects & Practice

# Mini Project: Calculator


def calculator(a, b, op):
if op == "+":
return a + b
elif op == "-":
return a - b
elif op == "*":
return a * b
elif op == "/":
return a / b

print(calculator(10, 5, "+"))

# Practice: Reverse a string


print("Python"[::-1])

You might also like