Python Tutorial for Beginners
Python is a versatile, easy-to-learn programming language that is widely used in various domains
like web development, data science, artificial intelligence, and more. This tutorial will help you get
started with Python by introducing basic concepts with examples.
1. Setting Up Python
Before writing Python code, you need to install Python on your system. You can download it from
Python's official website. Once installed, you can write Python code using any text editor or an
integrated development environment (IDE) like PyCharm, VS Code, or even the built-in IDLE.
2. Python Basics
a. Printing Output
The print() function is used to display output.
print("Hello, World!")
Output:
Hello, World!
b. Variables and Data Types
Variables store data values, and Python supports multiple data types like integers, floats, strings, etc.
name = "John"
age = 25
height = 5.8
is_student = True
print(name, age, height, is_student)
Output:
John 25 5.8 True
c. Comments
Comments are lines in the code that Python ignores. Use # for single-line comments.
# This is a single-line comment
print("Learning Python is fun!")
3. Control Structures
a. Conditional Statements
Use if, elif, and else to make decisions in your code.
num = 10
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output:
Positive number
b. Loops
i. for Loop
Iterates over a sequence (like a list or a range).
for i in range(5):
print(i)
Output:
0
1
2
3
4
ii. while Loop
Executes as long as the condition is True.
count = 0
while count < 5:
print(count)
count += 1
Output:
0
1
2
3
4
4. Functions
Functions are reusable blocks of code that perform a specific task.
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Output:
Hello, Alice!
5. Working with Lists
Lists are used to store multiple items in a single variable.
fruits = ["apple", "banana", "cherry"]
print(fruits)
fruits.append("orange") # Adding an item
print(fruits)
fruits.remove("banana") # Removing an item
print(fruits)
Output:
['apple', 'banana', 'cherry']
['apple', 'banana', 'cherry', 'orange']
['apple', 'cherry', 'orange']
6. Dictionaries
Dictionaries store data as key-value pairs.
person = {"name": "John", "age": 30, "city": "New York"}
print(person)
print(person["name"])
person["age"] = 31 # Updating a value
print(person)
Output:
{'name': 'John', 'age': 30, 'city': 'New York'}
John
{'name': 'John', 'age': 31, 'city': 'New York'}
7. Object-Oriented Programming (OOP)
Python supports OOP concepts like classes and objects.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"My name is {self.name} and I am {self.age} years old."
person = Person("Alice", 28)
print(person.introduce())
Output:
My name is Alice and I am 28 years old.
8. File Handling
Python allows you to read from and write to files.
Writing to a File
with open("example.txt", "w") as file:
file.write("This is a test file.")
Reading from a File
with open("example.txt", "r") as file:
content = file.read()
print(content)
Output:
This is a test file.
9. Libraries and Modules
Python has a rich set of libraries. You can import and use them.
import math
print(math.sqrt(16))
Output:
4.0
10. Next Steps
Once you master the basics, explore advanced topics like:
Working with APIs
Data Analysis using pandas
Machine Learning with scikit-learn
Web Development using Django or Flask