Introduction
Python is a beginner-friendly programming language known for its readability and simplicity. This step will help you set up Python, understand basic syntax, and write your first Python program.
1. Installing Python
Windows/macOS/Linux
- Download Python from the official site: python.org
- Install it and ensure
pip
(Python package manager) is included. - Verify installation:
or (on some systems)python --version
python3 --version
2. Running Python Code
You can run Python code using:
- Interactive Mode (REPL): Open terminal and type
python
. - Script Mode: Create a
.py
file and execute it:python script.py
3. Writing Your First Python Program
Create a file hello.py
and add:
print("Hello, Python!")
Run it:
python hello.py
4. Understanding Variables & Data Types
Variable Declaration
name = "Alice"
age = 25
is_python_learner = True
Data Types in Python
- String (
str
):"Hello"
- Integer (
int
):25
- Float (
float
):3.14
- Boolean (
bool
):True
orFalse
- List (
list
):[1, 2, 3]
- Dictionary (
dict
):{"name": "Alice", "age": 25}
Check data types:
print(type(name)) # Output: <class 'str'>
5. Working with Input & Output
Printing Output
print("Welcome to Python!")
Taking User Input
name = input("Enter your name: ")
print("Hello, " + name + "!")
6. Operators in Python
Arithmetic Operators
x = 10
y = 5
print(x + y, x - y, x * y, x / y, x % y)
Comparison Operators
print(10 > 5) # True
print(10 == "10") # False
7. Writing Comments in Python
- Single-line comment:
# This is a comment print("Hello")
- Multi-line comment:
""" This is a multi-line comment describing the code. """
8. Python Indentation
Python uses indentation instead of {}
:
if 5 > 2:
print("Five is greater than two!")
Exercises
- Install Python and verify the version.
- Run Python in interactive mode and try printing a message.
- Create a Python script that asks for a user's name and prints a greeting.
- Declare variables of different types and print their types.
- Write a small script using arithmetic operators.
Conclusion
You have now set up Python, learned basic syntax, and written your first program. The next step is to dive deeper into control flow with loops and conditionals.