7.
INTRODUCING PYTHON
Python is a high-level, interpreted, object-oriented, and general-purpose programming
language. It is widely used in areas such as web development, data science, artificial
intelligence, automation, and scientific computing due to its simplicity and readability.
7.1 Objectives of Python
The main objectives behind the development of Python are:
1. Simplicity: Python is designed to be easy to learn and use, even for beginners.
2. Readability: Python programs are easy to read and understand due to their English-
like syntax.
3. Productivity: Python enables programmers to write fewer lines of code compared to
other languages.
4. Portability: Python programs can run on different platforms such as Windows,
Linux, and macOS.
5. Extensibility: Python supports integration with other programming languages like C
and C++.
6. Versatility: Python is used in diverse domains such as data analysis, machine
learning, scripting, and application development.
7.2 Introduction to Python
Python was developed by Guido van Rossum in the late 1980s and was officially released in
1991. It follows an interpreted execution model, meaning code is executed line by line.
Features of Python
High-level language
Interpreted
Object-oriented
Dynamically typed
Extensive standard library
Free and open source
Python emphasizes code readability and simplicity, making it ideal for beginners as well as
professionals.
7.3 First Steps in Python
The first steps in Python involve understanding how to write and execute basic Python
programs.
Running Python
Python programs can be executed in:
Interactive mode
Script mode
First Python Program
The commonly written first program in Python is:
print("Hello, World!")
This statement displays the message on the screen. The print() function is used to output
text or values.
7.4 Basic Types in Python
Data types define the type of data stored in a variable.
Common Basic Data Types
1. int: Represents integer values
Example: a = 10
2. float: Represents decimal values
Example: b = 3.14
3. complex: Represents complex numbers
Example: c = 2 + 3j
4. str: Represents textual data
Example: name = "Python"
5. bool: Represents Boolean values (True or False)
Example: flag = True
Python is dynamically typed, meaning variable types are determined at runtime.
7.5 Basic Operations in Python
Python supports various operations to manipulate data.
Arithmetic Operations
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)
Exponentiation (**)
Relational Operations
<, >, <=, >=, ==, !=
Logical Operations
and
or
not
These operations help in decision-making and computations.
7.6 Functions in Python
A function is a block of reusable code designed to perform a specific task.
Types of Functions
1. Built-in functions (e.g., print(), len())
2. User-defined functions
Function Definition
Functions are defined using the def keyword.
def add(a, b):
return a + b
Advantages of Functions
Code reusability
Modularity
Easier debugging
Improved readability
7.7 Comments in Python
Comments are non-executable statements used to explain code.
Types of Comments
1. Single-line comments: Use #
2. # This is a comment
3. Multi-line comments: Written using triple quotes (''' or """)
Importance of Comments
Improve code readability
Help in documentation
Make maintenance easier
8. EFFECTIVE BUILDING BLOCKS
Effective building blocks are the fundamental concepts used to construct structured and
logical programs. They form the foundation of problem-solving in programming.
8.1 Logic
Logic refers to the systematic reasoning process used to solve problems.
Logical Thinking in Programming
Understanding the problem
Breaking it into smaller steps
Applying conditions and decisions
Python uses logical operators and conditional statements to implement logic.
8.2 Basic Arithmetic Constructs
Arithmetic constructs are used to perform mathematical calculations.
Common Arithmetic Constructs
Variables
Expressions
Operators
Example:
total = price * quantity
These constructs help in numerical problem-solving.
8.3 Program State
Program state refers to the current values of variables during program execution.
Characteristics of Program State
Variables change during execution
State depends on input and operations
Maintained in memory
Understanding program state helps in debugging and predicting program behavior.
8.4 Advanced Constructs
Advanced constructs are used to build complex and efficient programs.
Examples of Advanced Constructs
1. Conditional Statements
o if
o if-else
o nested if
2. Loops
o for loop
o while loop
3. Data Structures
o Lists
o Tuples
o Dictionaries
o Sets
4. Control Statements
o break
o continue
o pass
Importance of Advanced Constructs
Enable automation
Improve efficiency
Support complex problem-solving