Python Programming Basics
A Beginner's Study Guide
Table of Contents
1. Introduction to Python
2. Installing Python
3. Writing Your First Program
4. Variables and Data Types
5. User Input and Output
6. Operators
7. Conditional Statements
8. Loops
9. Functions
10. Lists, Tuples, Dictionaries, and Sets
11. Strings
12. File Handling
13. Exception Handling
14. Simple Practice Programs
15. Best Practices
16. Summary
Chapter 1: Introduction to Python
Python is a popular, high-level programming language known for its readability and simplicity. It was
created by Guido van Rossum and first released in 1991. Today, Python is used in many industries because it
supports rapid development and has a large collection of libraries.
Common Applications
• Web Development
• Automation
• Data Analysis
• Artificial Intelligence
• Machine Learning
• Scientific Computing
• Desktop Applications
• Game Development
Advantages
• Easy to learn
1
• Free and open source
• Cross-platform
• Large community support
• Rich library ecosystem
Chapter 2: Installing Python
Download Python from the official Python website.
After installation, verify it by opening a terminal or command prompt and typing:
python --version
or
python3 --version
If Python is installed correctly, the version number will be displayed.
Chapter 3: Writing Your First Program
Every beginner starts with the classic "Hello, World!" program.
print("Hello, World!")
Output:
Hello, World!
The print() function displays text or values on the screen.
Chapter 4: Variables and Data Types
Variables store information that can be used later in a program.
Example:
2
name = "Alice"
age = 21
height = 5.7
student = True
Python automatically determines the variable type.
Common Data Types
• Integer ( int )
• Floating-point ( float )
• String ( str )
• Boolean ( bool )
Example:
marks = 95
temperature = 36.5
city = "Pune"
passed = True
Chapter 5: User Input and Output
Input allows users to enter information.
name = input("Enter your name: ")
print("Welcome", name)
Reading integers:
age = int(input("Enter age: "))
Reading decimal values:
salary = float(input("Enter salary: "))
3
Chapter 6: Operators
Arithmetic Operators
+
-
*
/
%
**
//
Example:
a = 15
b = 4
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)
Comparison Operators
==
!=
>
<
>=
<=
Example:
print(10 > 5)
Output:
True
4
Logical Operators
• and
• or
• not
Example:
age = 20
if age > 18 and age < 60:
print("Eligible")
Chapter 7: Conditional Statements
Decision making is performed using if , elif , and else .
Example:
marks = 72
if marks >= 75:
print("Distinction")
elif marks >= 60:
print("First Class")
elif marks >= 35:
print("Pass")
else:
print("Fail")
Nested condition example:
age = 25
if age >= 18:
if age >= 21:
print("Adult")
5
Chapter 8: Loops
For Loop
for i in range(1,6):
print(i)
Output:
1
2
3
4
5
Loop through a list:
colors = ["Red","Blue","Green"]
for color in colors:
print(color)
While Loop
count = 1
while count <= 5:
print(count)
count += 1
break Statement
for i in range(10):
if i == 6:
break
print(i)
6
continue Statement
for i in range(6):
if i == 3:
continue
print(i)
Chapter 9: Functions
Functions organize reusable code.
Example:
def greet():
print("Welcome to Python")
greet()
Function with parameters:
def square(number):
return number * number
print(square(8))
Chapter 10: Collections
Lists
numbers = [10,20,30,40]
Useful methods:
[Link](50)
[Link](20)
7
Tuples
days = ("Mon","Tue","Wed")
Tuples cannot be modified after creation.
Dictionaries
student = {
"name":"Riya",
"age":20
}
print(student["name"])
Sets
languages = {"Python","Java","C++"}
Sets automatically remove duplicate values.
Chapter 11: Strings
Strings store text.
language = "Python"
Useful operations:
print([Link]())
print([Link]())
print(len(language))
String slicing:
text = "Programming"
8
print(text[:5])
print(text[-3:])
Chapter 12: File Handling
Writing to a file:
file = open("[Link]","w")
[Link]("Learning Python")
[Link]()
Reading a file:
file = open("[Link]","r")
print([Link]())
[Link]()
Using a context manager:
with open("[Link]","r") as file:
print([Link]())
Chapter 13: Exception Handling
Exceptions prevent programs from crashing unexpectedly.
Example:
try:
number = int(input("Enter a number: "))
print(100 / number)
except ZeroDivisionError:
print("Cannot divide by zero.")
9
except ValueError:
print("Invalid input.")
Chapter 14: Practice Programs
Program 1: Even or Odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Program 2: Largest of Two Numbers
a = int(input())
b = int(input())
if a > b:
print(a)
else:
print(b)
Program 3: Multiplication Table
num = 7
for i in range(1,11):
print(num, "x", i, "=", num*i)
Program 4: Sum of Numbers
total = 0
for i in range(1,101):
total += i
print(total)
10
Program 5: Factorial
number = 5
fact = 1
for i in range(1, number+1):
fact *= i
print(fact)
Chapter 15: Best Practices
• Use meaningful variable names.
• Keep functions short and focused.
• Add comments where necessary.
• Follow consistent indentation.
• Avoid repeating code.
• Test your programs with different inputs.
• Handle errors gracefully.
Chapter 16: Summary
Python is an excellent programming language for beginners because it emphasizes readable syntax and
practical problem solving. Learning variables, operators, conditions, loops, functions, collections, file
handling, and exception handling provides a strong foundation for more advanced topics such as object-
oriented programming, web development, automation, data science, and machine learning.
Regular practice is the most effective way to improve Python programming skills. Start with simple
programs, gradually solve more challenging problems, and build small projects to reinforce your
understanding.
11