Chapter 1: Introduction to Python
1.1 What is Python?
Python is a high-level, general-purpose programming language used to create software, websites, automation tools, data-analysis applications, artificial intelligence systems, and many other types of programs.
Python is designed with a simple and readable syntax, which makes it a good language for beginners. At the same time, it provides powerful features for developing large and complex applications.
Key Features of Python
Easy to learn: Python syntax is relatively simple and readable.
High-level language: Programmers can work with concepts without managing low-level computer operations.
Interpreted: Python programs are generally executed by the Python interpreter.
Cross-platform: Python programs can run on Windows, Linux, macOS, and other supported platforms.
Open source: Python is freely available for learning and development.
Large standard library: Python includes many modules for common programming tasks.
Extensible: Python can work with libraries and technologies written in other programming languages.
Supports multiple programming styles: It supports procedural, object-oriented, and functional programming approaches.
1.2 Why Learn Python?
Python is useful for both beginners and experienced developers because it can be applied to many areas of technology.
Common Uses of Python
| Area | Example Uses |
|---|---|
| Web Development | Creating websites and web applications |
| Data Science | Processing and analyzing data |
| Artificial Intelligence | Machine learning and AI applications |
| Automation | Automating repetitive tasks |
| Education | Learning programming concepts |
| Desktop Applications | Creating software with graphical interfaces |
| Scientific Computing | Mathematical and scientific calculations |
| Cybersecurity | Security analysis and automation |
| Game Development | Creating simple games and prototypes |
Python is also widely used for scripting and automation because programs can often be developed with relatively little code.
1.3 History of Python
Python was created by Guido van Rossum and first released publicly in the early 1990s.
The language was designed with an emphasis on code readability and programmer productivity. Over time, Python developed into a widely used programming language with a large ecosystem of libraries and frameworks.
Python 2 and Python 3 were separate major versions for many years. Python 2 eventually reached the end of official support, so modern Python learning and development should focus on Python 3.
1.4 Python Program Structure
A Python program can be very simple. For example:
print("Hello, World!")
The print() function displays information on the screen.
Output
Hello, World!
This is one of the simplest Python programs and is often used to demonstrate that the Python environment is working correctly.
1.5 Python Syntax
Syntax means the rules used to write a program correctly.
Python uses indentation to represent blocks of code. This makes the structure of a program visually clear.
Example:
age = 18 if age >= 18: print("You are an adult.")
The spaces before print() are important because they indicate that the statement belongs to the if block.
Important Rule
Use consistent indentation in Python. Four spaces are commonly used for each indentation level.
Incorrect indentation can result in an error.
1.6 Comments in Python
Comments are notes written inside a program for humans. Python does not execute them as normal program statements.
A single-line comment begins with #.
# This program displays a greeting print("Welcome to Python")
Comments are useful for explaining code and making programs easier to understand.
Example
# Store the student's age age = 20 # Display the age print(age)
Good comments should explain something useful rather than simply repeating the code.
1.7 Python Variables
A variable is a name used to refer to a value.
Example:
name = "Rahul" age = 21
Here:
name refers to the text "Rahul".
age refers to the number 21.
Python does not require you to declare the data type separately when creating a normal variable.
Example
student_name = "Aman" marks = 85 percentage = 85.5
Python determines the type of value from the data assigned to the variable.
1.8 Rules for Naming Variables
Python variable names should follow these rules:
A variable name can contain letters, numbers, and underscores.
A variable name cannot start with a number.
Spaces are not allowed in variable names.
Python keywords should not be used as variable names.
Variable names are case-sensitive.
Valid Names
name = "Aman" student_name = "Aman" age2 = 20 total_marks = 450
Invalid Names
2age = 20 student name = "Aman"
A clear naming style makes programs easier to read.
1.9 Basic Data Types
A data type describes the kind of value stored or processed by a program.
Some common Python data types are:
Integer (int)
Used for whole numbers.
age = 25
Floating-Point Number (float)
Used for numbers containing a decimal part.
price = 99.50
String (str)
Used for text.
name = "Gwalnet"
Boolean (bool)
Represents either True or False.
is_logged_in = True
Python provides many additional data types, which will be studied in later chapters.
1.10 Checking the Data Type
The type() function can be used to find the type of a value.
age = 25 print(type(age))
Output:
<class 'int'>
Another example:
name = "Python" print(type(name))
Output:
<class 'str'>
1.11 Taking Input from the User
Python provides the input() function to receive information from the user.
name = input("Enter your name: ") print("Hello", name)
If the user enters:
Aman
The program can display:
Hello Aman
Important Point
The value returned by input() is normally a string.
If a numerical value is required, it can be converted.
age = int(input("Enter your age: ")) print(age)
1.12 Type Conversion
Type conversion means changing a value from one data type to another.
Common conversion functions include:
int() – converts a value to an integer when possible
float() – converts a value to a floating-point number
str() – converts a value to a string
bool() – converts a value to a Boolean value
Example
number = "25" number = int(number) print(number + 5)
Output:
30
1.13 Basic Operators
Operators are symbols or keywords used to perform operations.
Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| - | Subtraction | 10 - 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| // | Floor Division | 10 // 3 |
| % | Remainder | 10 % 3 |
| ** | Power | 2 ** 3 |
Example:
a = 10 b = 3 print(a + b) print(a - b) print(a * b) print(a / b) print(a % b)
1.14 A Simple Python Program
The following program accepts two numbers and calculates their sum.
first = int(input("Enter first number: ")) second = int(input("Enter second number: ")) total = first + second print("Sum =", total)
How It Works
The program asks the user for the first number.
It asks for the second number.
int() converts the entered values into integers.
The + operator calculates the sum.
print() displays the result.
1.15 Python Interpreter
The Python interpreter reads and executes Python instructions.
Python can commonly be used in two ways:
Interactive Mode
You can enter a statement and immediately see its result.
Example:
>>> 5 + 3 8
Script Mode
You can save Python instructions in a file with a .py extension.
Example:
hello.py
The file can contain:
print("Welcome to Python")
This approach is useful for creating complete programs.
1.16 Python Development Tools
Python programs can be written using many different editors and development environments.
Examples include:
Python's interactive interpreter
IDLE
Visual Studio Code
PyCharm
Jupyter Notebook
Other text editors and IDEs that support Python
For beginners, a simple editor or beginner-friendly IDE is often sufficient.
1.17 Common Beginner Mistakes
Mistake 1: Incorrect indentation
if age >= 18: print("Adult")
The statement inside the if block needs indentation.
Correct:
if age >= 18: print("Adult")
Mistake 2: Forgetting quotes around text
Incorrect:
name = Aman
Correct:
name = "Aman"
Mistake 3: Mixing text and numbers incorrectly
For example:
age = 20 print("Age: " + age)
This can cause a type-related error because "Age: " is a string while age is an integer.
A simple solution is:
print("Age:", age)
Chapter 1 Summary
In this chapter, you learned:
What Python is
Important features of Python
Common applications of Python
A brief history of Python
Basic Python syntax
Comments
Variables
Variable naming rules
Basic data types
The type() function
User input
Type conversion
Arithmetic operators
The Python interpreter
Python scripts
Common beginner mistakes
Quick Revision
Q1. What is Python?
Python is a high-level, general-purpose programming language known for its readable syntax and wide range of applications.
Q2. Who created Python?
Python was created by Guido van Rossum.
Q3. Which function is used to display output?
The print() function.
Q4. Which function is used to receive input from a user?
The input() function.
Q5. Which symbol is used to create a single-line comment?
The # symbol.
Q6. Is Python case-sensitive?
Yes. For example, Name and name are treated as different identifiers.
Q7. What is the extension of a Python source file?
.py
Practice Programs
Program 1: Display Your Name
name = input("Enter your name: ") print("Welcome,", name)
Program 2: Calculate the Area of a Rectangle
length = float(input("Enter length: ")) width = float(input("Enter width: ")) area = length * width print("Area =", area)
Program 3: Calculate the Average of Three Numbers
a = float(input("Enter first number: ")) b = float(input("Enter second number: ")) c = float(input("Enter third number: ")) average = (a + b + c) / 3 print("Average =", average)
Chapter 1 Activity
Try creating a Python program that asks the user for:
Name
Age
City
Then display all three values in a clear format.
Example output:
Name: Aman Age: 20 City: Delhi
Next Chapter: Python Variables, Data Types and Type Conversion