Python Tutorial for Beginners: Variables, Data Types & Control Flow
Python is one of the world's most popular and beginner-friendly programming languages. Used in web development, data science, artificial intelligence, automation, and more — Python's clean, readable syntax makes it an ideal first language for aspiring programmers. This tutorial will guide you through your first Python steps: variables, data types, operators, and control flow.
You can practice all the examples in this tutorial right in your browser using our Try Python tool — no installation needed!
Why Learn Python?
Before we dive in, let's understand why Python is such a popular choice:
- Easy to read: Python code looks almost like plain English, making it easy to understand.
- Versatile: Used in web development (Django, Flask), data science (Pandas, NumPy), AI/ML (TensorFlow, PyTorch), and automation.
- Large community: Massive ecosystem of libraries and a supportive community.
- High demand: Python developers are among the most sought-after in the job market.
- Free and open source: Python is completely free to use and distribute.
Your First Python Program
Let's start with the classic "Hello, World!" program. In Python, it's just one line:
print("Hello, World!")
The print() function displays output to the screen. Simple, right? Try typing this in
our Try Python editor and click Run!
Variables and Assignment
Variables are used to store data. In Python, you don't need to declare the type of a variable — Python figures it out automatically:
# Creating variables name = "Alice" age = 25 height = 5.6 is_student = True print(name) # Alice print(age) # 25 print(height) # 5.6 print(is_student) # True
Variable names should be descriptive, start with a letter or underscore, and use lowercase with
underscores for multiple words (e.g., first_name, total_price).
Python Data Types
Python has several built-in data types:
1. Strings (str)
Strings are sequences of characters enclosed in single or double quotes:
greeting = "Hello, Python!" language = 'Python' # String operations print(len(greeting)) # 15 (length) print(greeting.upper()) # HELLO, PYTHON! print(greeting.replace("Hello", "Hi")) # Hi, Python! # f-strings (modern way to format strings) name = "Alice" age = 25 print(f"My name is {name} and I am {age} years old.")
2. Numbers (int and float)
# Integers (whole numbers) x = 10 y = -5 z = 0 # Floats (decimal numbers) pi = 3.14159 temperature = -17.5 # Arithmetic operations print(10 + 3) # Addition: 13 print(10 - 3) # Subtraction: 7 print(10 * 3) # Multiplication: 30 print(10 / 3) # Division: 3.3333... print(10 // 3) # Integer division: 3 print(10 % 3) # Modulus (remainder): 1 print(2 ** 8) # Power: 256
3. Booleans (bool)
is_raining = True is_sunny = False print(10 > 5) # True print(10 == 10) # True print(10 != 5) # True print(10 < 5) # False
4. Lists
Lists store multiple items in a single variable:
fruits = ["apple", "banana", "cherry"] numbers = [1, 2, 3, 4, 5] mixed = ["hello", 42, 3.14, True] print(fruits[0]) # apple (first item) print(fruits[-1]) # cherry (last item) print(len(fruits)) # 3 fruits.append("mango") # Add to end fruits.remove("banana")# Remove item print(fruits) # ['apple', 'cherry', 'mango']
Control Flow: if / elif / else
Control flow lets your program make decisions:
age = 18 if age < 13: print("Child") elif age < 18: print("Teenager") elif age == 18: print("Just turned adult!") else: print("Adult") # Output: Just turned adult!
Note: Python uses indentation (spaces or tabs) to define code blocks. This is not optional — it's part of Python's syntax.
Loops
for loops
# Iterate over a list fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # Use range() for number sequences for i in range(5): print(i) # Prints 0, 1, 2, 3, 4 # range(start, stop, step) for i in range(1, 10, 2): print(i) # Prints 1, 3, 5, 7, 9
while loops
count = 0 while count < 5: print(f"Count: {count}") count += 1 # Increment count # Output: Count: 0, Count: 1, ..., Count: 4
Functions
Functions let you organize and reuse code:
def greet(name): """This function greets a person by name.""" return f"Hello, {name}! Welcome to Python." # Call the function message = greet("Alice") print(message) # Hello, Alice! Welcome to Python. # Function with default parameter def greet(name, language="Python"): return f"Welcome to {language}, {name}!" print(greet("Bob")) # Welcome to Python, Bob! print(greet("Carol", "Java")) # Welcome to Java, Carol!
What's Next?
Congratulations! You now know the fundamentals of Python. Here's what to learn next:
- Dictionaries and Sets — More data structures for storing key-value pairs.
- File I/O — Reading and writing files with Python.
- Object-Oriented Programming — Classes and objects in Python.
- Libraries — Explore NumPy, Pandas, Matplotlib for data science.
- Web Frameworks — Learn Django or Flask for web development.
Practice Python Right in Your Browser!
Use Feature Gen's free online Python editor to practice all these examples without installing anything.
Try Python Now →