Module 1: Introduction to Python
Welcome to Module 1 of the Python Programming Course.
Python is one of the most popular programming languages in the world. It is beginner-friendly, readable, and powerful enough to build web applications, automation scripts, artificial intelligence systems, data science applications, APIs, and much more.
In this module, you will learn the fundamentals of Python and write your first Python programs.
By the end of this lesson, you will understand:
- What Python is
- The history of Python
- Why Python is popular
- Important Python features
- Real-world Python applications
- How to install Python
- How to install VS Code
- How to run Python programs
- Python interactive mode and script mode
- The
print()function - Python comments
- Keywords and identifiers
- Variables and naming conventions
What Is Python?
Python is a high-level, general-purpose programming language known for its simple and readable syntax.
Python supports several programming paradigms, including:
- Procedural programming
- Object-oriented programming
- Functional programming
One of Python's biggest advantages is that beginners can write useful programs with relatively little code.
For example:
1print("Hello, Python!")
Output:
1Hello, Python!
The print() function sends text or other values to the program's standard output.
Python is also dynamically typed, which means you generally do not need to declare a variable's type explicitly.
1name = "Alice" 2age = 22
Python determines the appropriate types from the assigned values.
History of Python
Python was created by Guido van Rossum and was first released publicly in 1991.
The language was designed with a strong emphasis on:
- Readability
- Simplicity
- Developer productivity
- Clear syntax
Python later evolved through several major versions.
The most important distinction for modern developers is between Python 2 and Python 3. Python 2 reached its official end of life in 2020, so new projects should use Python 3.
Today, Python is widely used in software development, automation, data science, artificial intelligence, education, and research.
Why Learn Python?
Python is an excellent programming language for beginners because its syntax is relatively easy to read and understand.
Consider this example:
1for number in range(1, 6): 2 print(number)
Output:
11 22 33 44 55
The code is short while still being easy to understand.
Some major reasons to learn Python include:
Easy-to-Read Syntax
Python code often looks close to plain English.
1name = "Alice" 2 3if name == "Alice": 4 print("Welcome, Alice!")
Large Ecosystem
Python has thousands of libraries and frameworks for different areas of development.
For example:
- Django and Flask for web development
- NumPy and pandas for data analysis
- PyTorch and TensorFlow for machine learning
- Requests for HTTP requests
- FastAPI for APIs
- Selenium and Playwright for browser automation
Cross-Platform
Python programs can run on major operating systems such as:
- Windows
- Linux
- macOS
Strong Community
Python has a large global developer community, which means beginners can find extensive documentation, tutorials, libraries, and troubleshooting resources.
Features of Python
Python provides several features that make it useful for both beginners and professional developers.
Simple and Readable Syntax
Python uses indentation to define code blocks.
1age = 20 2 3if age >= 18: 4 print("Adult")
The indentation is part of Python's syntax and is important for correctly structuring programs.
Dynamically Typed
You do not normally specify the variable type when assigning a value.
1name = "Alice" 2age = 25 3price = 99.99
Python associates each value with its type automatically.
Object-Oriented
Python supports object-oriented programming using classes and objects.
1class Student: 2 def __init__(self, name): 3 self.name = name 4 5student = Student("Alice") 6 7print(student.name)
Output:
1Alice
Automatic Memory Management
Python automatically manages memory and provides garbage collection to help reclaim memory that is no longer being used.
Large Standard Library
Python includes a large collection of built-in modules for tasks such as:
- Working with files
- Dates and times
- Mathematics
- JSON
- Operating system operations
- Regular expressions
- Networking
Portable
Python programs can generally be moved between operating systems with little or no modification, although platform-specific dependencies can sometimes require changes.
Real-World Applications of Python
Python is used across many areas of technology.
Web Development
Frameworks such as Django, Flask, and FastAPI can be used to build websites and backend APIs.
Example:
1from fastapi import FastAPI 2 3app = FastAPI() 4 5@app.get("/") 6def home(): 7 return {"message": "Hello, Python!"}
Artificial Intelligence and Machine Learning
Python is one of the most widely used languages for AI and machine learning.
Popular libraries include:
- PyTorch
- TensorFlow
- scikit-learn
- NumPy
- pandas
Data Science
Python is frequently used for:
- Data cleaning
- Data analysis
- Data visualization
- Statistical computing
- Machine learning
Automation
Python can automate repetitive tasks.
1for task_number in range(1, 4): 2 print(f"Task {task_number} completed")
Output:
1Task 1 completed 2Task 2 completed 3Task 3 completed
Cybersecurity
Python can be used to create security tools, automate security tasks, analyze logs, process network data, and build defensive security utilities.
Web Scraping
Python libraries can be used to retrieve and process information from websites where scraping is permitted.
APIs and Backend Development
Python can be used to build REST APIs and backend services.
Robotics and IoT
Python is also used in robotics, Raspberry Pi projects, hardware automation, and Internet of Things applications.
Installing Python
Before writing Python programs, you need to install Python 3.
Download Python from the official Python website.
Download Python from python.org
After installation, open a terminal or command prompt and verify the installation.
Windows
1python --version
If your system uses the Python launcher, you can also try:
1py --version
Linux and macOS
1python3 --version
Example output:
1Python 3.13.2
Your exact version may be different because Python releases continue to change.
Add Python to PATH on Windows
During Windows installation, enable the option to add Python to your PATH when available.
This allows commands such as:
1python
to be executed directly from the terminal.
Installing Visual Studio Code
Visual Studio Code (VS Code) is a lightweight source-code editor that works well for Python development.
After installing VS Code:
- Open VS Code.
- Open the Extensions panel.
- Search for the official Python extension.
- Install the extension.
- Open or create a Python file.
Useful extensions and tools include:
- Python
- Pylance
- Black Formatter
For beginners, the Python and Pylance extensions are especially useful for code completion, error detection, debugging, and type information.
Installing PyCharm
PyCharm is a Python-focused integrated development environment (IDE) developed by JetBrains.
PyCharm provides features such as:
- Intelligent code completion
- Debugging
- Refactoring
- Git integration
- Integrated terminal
- Project management
- Python environment management
You do not need both VS Code and PyCharm. Beginners can choose either editor based on their preference.
Running Python Programs
There are several ways to run Python code.
Common options include:
- Python interactive interpreter
- Terminal or command prompt
- VS Code
- PyCharm
- Jupyter Notebook
For beginners, starting with the terminal and a .py file is a good way to understand how Python programs work.
Python Interactive Interpreter
The Python interpreter provides an interactive environment where you can enter Python expressions and immediately see their results.
Open a terminal and run:
1python
On systems where Python 3 uses the python3 command:
1python3
You may see something similar to:
1Python 3.13.2 2>>>
The >>> prompt means Python is ready to execute your command.
Try:
1>>> 5 + 8 213
Another example:
1>>> name = "Alice" 2>>> print(name) 3Alice
The interactive interpreter is useful for:
- Learning Python
- Testing small expressions
- Checking syntax
- Experimenting with functions
- Debugging simple problems
To exit:
1>>> exit()
You can also use Ctrl+D on Linux/macOS or Ctrl+Z followed by Enter on Windows.
Interactive Mode vs Script Mode
Python can commonly be used in two basic ways: interactive mode and script mode.
Interactive Mode
You enter Python instructions directly into the interpreter.
1>>> 10 + 20 230 3 4>>> 7 * 8 556
This is useful when you want to test something quickly.
Script Mode
In script mode, you save Python code in a file with the .py extension.
Create a file named:
1hello.py
Add:
1print("Welcome to Python")
Then run it from the terminal.
Windows:
1python hello.py
Linux/macOS:
1python3 hello.py
Output:
1Welcome to Python
For larger programs, script mode is much more practical because your code can be saved, edited, tested, and reused.
Your First Python Program
Let's write a simple Python program.
Create a file named:
1hello.py
Add this code:
1print("Hello, World!")
Run the program:
1python hello.py
Output:
1Hello, World!
Congratulations! You have written your first Python program.
Understanding the print() Function
The print() function displays values in the program's standard output.
Example:
1print("Hello World") 2print("Welcome to Python") 3print("Happy Coding!")
Output:
1Hello World 2Welcome to Python 3Happy Coding!
You can also print numbers:
1print(10) 2print(25 + 15) 3print(100 / 4)
Output:
110 240 325.0
You can print multiple values:
1name = "Alice" 2age = 22 3 4print("Name:", name) 5print("Age:", age)
Output:
1Name: Alice 2Age: 22
Using f-Strings
For formatted output, Python provides f-strings.
1name = "Alice" 2age = 22 3 4print(f"My name is {name} and I am {age} years old.")
Output:
1My name is Alice and I am 22 years old.
You will learn more about formatted strings later in the course.
Python Comments
Comments are notes written in source code to explain what the code does.
Python ignores comments during normal program execution.
A single-line comment begins with #.
1# Store the user's name 2name = "Alice" 3 4print(name)
Comments are useful for explaining complex logic and documenting important decisions.
Multi-Line Comments
Python does not have a dedicated multiline comment syntax like some other languages.
Triple-quoted strings can span multiple lines:
1""" 2This is a multi-line string. 3It is not technically a comment. 4"""
When used as a standalone expression, Python does not assign this string to a variable. Developers sometimes use this style as a comment, but docstrings are the intended way to document functions, classes, and modules.
Example:
1def greet(name): 2 """Return a greeting for the supplied name.""" 3 return f"Hello, {name}!"
The string inside the function is a docstring, not simply a comment.
Python Keywords
Keywords are reserved words that have special meanings in Python.
Some Python keywords include:
1if 2else 3elif 4for 5while 6class 7def 8return 9import 10from 11try 12except 13with 14True 15False 16None
You cannot use a Python keyword as a variable name.
Incorrect:
1class = 10
This produces a syntax error because class is a reserved keyword.
Correct:
1class_name = "Python"
You can see Python's keyword list programmatically:
1import keyword 2 3print(keyword.kwlist)
This is useful when working with different Python versions because the keyword list can change over time.
Python Identifiers
An identifier is a name used to identify a programming element such as a variable, function, class, or module.
Examples:
1student_name = "Alice" 2total_marks = 450 3 4def calculate_total(): 5 pass 6 7class Student: 8 pass
Here:
student_nameis a variable identifier.total_marksis a variable identifier.calculate_totalis a function identifier.Studentis a class identifier.
Rules for Python Identifiers
A Python identifier:
- Can contain letters.
- Can contain numbers after the first character.
- Can contain underscores.
- Cannot begin with a number.
- Cannot contain spaces.
- Cannot contain characters such as
-or@. - Cannot be a reserved keyword.
- Is case-sensitive.
Valid examples:
1student 2student_name 3age1 4total_marks 5_user_name
Invalid examples:
11student 2student-name 3student name 4class
Python Variables
A variable is a name that refers to a value.
Example:
1name = "John" 2age = 21 3height = 5.8 4is_student = True 5 6print(name) 7print(age) 8print(height) 9print(is_student)
Output:
1John 221 35.8 4True
Python does not require you to declare the type before assigning a value.
For example:
1name = "Alice"
Python creates a string value and binds the name name to that value.
You can inspect the type using type():
1name = "Alice" 2age = 22 3 4print(type(name)) 5print(type(age))
Output:
1<class 'str'> 2<class 'int'>
The exact representation may vary slightly between Python environments, but str and int indicate the respective data types.
Assigning Multiple Variables
Python allows multiple assignments in a single statement.
1name, age, city = "Alice", 22, "Delhi" 2 3print(name) 4print(age) 5print(city)
Output:
1Alice 222 3Delhi
You can also assign the same value to multiple variables:
1x = y = z = 0 2 3print(x) 4print(y) 5print(z)
Output:
10 20 30
Python Variable Naming Convention
Meaningful variable names make programs easier to read and maintain.
Prefer:
1student_name = "Alice" 2total_marks = 450 3user_age = 22 4is_logged_in = True
Avoid unclear names when they do not communicate meaning:
1a = "Alice" 2b = 450 3c = 22
Python's common naming convention for variables and functions is snake_case.
Example:
1first_name = "Alice" 2last_name = "Smith" 3 4full_name = first_name + " " + last_name 5 6print(full_name)
Output:
1Alice Smith
For constants, Python commonly uses uppercase names:
1MAX_USERS = 100 2PI = 3.14159
These are conventions rather than enforced language rules.
Common Beginner Mistakes
When learning Python, beginners often encounter a few common problems.
Forgetting Indentation
Incorrect:
1age = 20 2 3if age >= 18: 4print("Adult")
Correct:
1age = 20 2 3if age >= 18: 4 print("Adult")
Using a Keyword as a Variable
Incorrect:
1for = 10
Correct:
1loop_count = 10
Starting a Variable Name With a Number
Incorrect:
11name = "Alice"
Correct:
1name1 = "Alice"
Using Hyphens in Identifiers
Incorrect:
1student-name = "Alice"
Correct:
1student_name = "Alice"
Practical Beginner Project
Let's combine several concepts from this module.
Create a file named:
1student_profile.py
Add:
1# Student profile program 2 3name = "Alice" 4age = 21 5city = "Delhi" 6course = "Python Programming" 7 8print("Student Profile") 9print("----------------") 10print(f"Name: {name}") 11print(f"Age: {age}") 12print(f"City: {city}") 13print(f"Course: {course}")
Output:
1Student Profile 2---------------- 3Name: Alice 4Age: 21 5City: Delhi 6Course: Python Programming
This small program demonstrates:
- Comments
- Variables
- Strings
- Integers
print()- f-strings
- Meaningful variable names
Module Summary
In this module, you learned the fundamentals required to begin programming with Python.
You learned:
- What Python is
- Python's history
- Why Python is popular
- Important Python features
- Real-world Python applications
- How to install Python
- How to verify a Python installation
- How to install VS Code
- How to install PyCharm
- How to run Python programs
- Python's interactive interpreter
- Interactive mode
- Script mode
- The
.pyfile extension - The
print()function - Comments and docstrings
- Python keywords
- Identifiers
- Variables
- Variable naming conventions
- Common beginner mistakes
These concepts provide the foundation for the rest of the course.
Practice Exercises
Try these exercises without looking at the solution first.
Exercise 1: Check Python
Install Python and verify your Python 3 version from the terminal.
Exercise 2: Hello Python
Create a file named hello.py and print:
1Hello, Python! 2I am learning programming.
Exercise 3: Student Information
Create variables for:
- Name
- Age
- City
- Course
Then display them using print().
Exercise 4: Personal Introduction
Create a program that prints a sentence containing your name, age, and favorite programming language.
Exercise 5: Comments
Write a Python program containing at least three useful comments.
Exercise 6: Variables
Create five variables using meaningful names and display their values.
Exercise 7: Identifier Practice
Identify which of the following are valid Python identifiers:
1student_name 22students 3total_marks 4student-name 5_user 6class 7age2
Exercise 8: Interactive Mode
Open the Python interpreter and calculate:
125 + 75 212 * 8 3100 / 4 42 ** 5
Exercise 9: Automation Practice
Write a loop that prints:
1Task 1 completed 2Task 2 completed 3Task 3 completed 4Task 4 completed 5Task 5 completed
Exercise 10: Student Profile
Create your own student profile program using variables, comments, and f-strings.
Quiz
Question 1: Who created Python?
A. James Gosling B. Dennis Ritchie C. Guido van Rossum D. Bjarne Stroustrup
Answer: C. Guido van Rossum
Question 2: Which file extension is normally used for Python source files?
A. .java
B. .py
C. .cpp
D. .js
Answer: B. .py
Question 3: Which function is commonly used to display output?
1print()
Question 4: Which symbol starts a single-line comment in Python?
A. //
B. /*
C. #
D. --
Answer: C. #
Question 5: Which is a valid Python variable name?
A. 2name
B. student-name
C. student_name
D. student name
Answer: C. student_name
Question 6: Is Python dynamically typed?
Answer: Yes. Python determines the type of a value at runtime, and variables do not normally require explicit type declarations.
What's Next?
In the next module, you will learn about Python Variables and Data Types in greater detail.
You will explore:
- Python's built-in data types
- Integers
- Floating-point numbers
- Strings
- Booleans
- Lists
- Tuples
- Sets
- Dictionaries
type()- Type conversion
- User input
- Output formatting
These concepts will help you move from simple Python programs to interactive programs that can process and manipulate real data.