Introduction to Python:
Python is a high-level, interpreted, interactive and object-oriented
scripting language.
o Python is Interpreted: Python is processed at runtime by the interpreter.
o Python is Interactive
o Python is Object-Oriented: Python supports Object-Oriented style or
technique of programming.
o Python is a Beginner’s Language: Python is a great language for the
beginner-level programmers.
History of Python:
Python was developed by “Guido Van Rossum” in 1971.
Python is derived from many other languages, including ABC, Modula-3,
C, C++, Algol-68, SmallTalk, UNIX shell, and other scripting languages.
Python was first released publicly in 1991 as Python 0.9.0, which
included core features such as data types, functions, and modules.
In 2000, Python 2.0 was introduced with enhancements like list
comprehensions and garbage collection.
History of Python:
Later, Python 3.0 was released in 2008 to remove design flaws and
improve consistency.
Today, Python is widely used in
o web development
o data science
o artificial intelligence
o Automation
o cloud computing.
Features of Python:
Easy to Learn: Python has a small number of keywords, a simple
structure, and clear syntax, making it easy for beginners to learn quickly.
Easy to Read: Python programs are easy to read and understand because
the code is clean and well-defined.
Easy to Maintain: Python source code is easy to maintain and modify,
even for large programs.
Features of Python:
Rich Standard Library: Python provides a large standard library that is
portable and works across different platforms such as UNIX and
Windows.
Interactive Mode: Python supports an interactive mode that allows
programmers to test and debug code line by line.
Portable: Python programs can run on different hardware platforms with
the same interface.
Features of Python:
Extendable: Python allows the addition of low-level modules to the
interpreter to improve performance or add new features.
Database Support: Python provides interfaces to major commercial data
bases.
GUI Programming: Python supports GUI application development and
can be used with systems such as Windows, macOS, and UNIX.
Features of Python:
Scalable: Python is suitable for both small scripts and large applications
due to its good program structure.
Dynamically Typed: Python does not require variable types to be
declared explicitly, as types are determined at runtime.
Applications of Python:
Web Development: Python is widely used to develop web applications
using frameworks such as Django, Flask, and FastAPI.
Data Science and Data Analytics: Python is extensively used for data
analysis, data visualization, and statistics using libraries like NumPy,
Pandas, Matplotlib, and Seaborn.
Artificial Intelligence and Machine Learning: Python is a leading
language in AI and ML development with libraries such as TensorFlow,
Keras, PyTorch, and Scikit-learn.
Applications of Python:
Automation and Scripting: Python is used to automate repetitive tasks
such as file handling, data processing, and system administration.
Scientific and Engineering Applications: Python is used in scientific
computing, simulations, and research with libraries like SciPy and
SymPy.
Desktop GUI Applications: Python supports GUI development using
Tkinter, PyQt, and wxPython.
Applications of Python:
Game Development: Python is used to create games using libraries such as
Pygame.
Database Applications: Python is used to develop database-driven applications
and supports major databases like MySQL, Oracle, and SQLite.
Network Programming: Python is used in network applications, socket
programming, and network automation.
Cloud Computing and DevOps: Python is widely used in cloud platforms,
automation tools, and DevOps practices such as CI/CD pipelines.
Variables:
Variable is a memory location where value can be stored.
The value may be either string, number etc..
Variables must be assigned before using them.
No data type declaration is required for variable in Python.
The interpreter allocates memory based on data type of a variable.
Rules for Python Variables:
A variable name must start with a letter or the underscore character.
A variable name cannot start with a number.
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different
variables).
Assigning values to Variables:
The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and
the operand to the right of the = operator is the value stored in the
variable.
Multiple Assignments:
We can assign a single value to several variables simultaneously.
For example –
>>>a = b = c = 1
We can also assign multiple objects to multiple variables as shown below.
Keywords:
These are reserved words and you cannot use them as constant or
variable or any other identifier names.
Input/Output Functions:
input() is a built-in Python function used to take input from the user
during program execution.
Syntax: input(prompt string)
It reads data as a string from the user.
The program waits until the user enters a value and presses Enter.
The entered value is returned as a string.
Here, prompt string is optional.
Input/Output Functions:
Input/Output Functions:
In Python, the print() function is used to display output on the screen.
Syntax: print(value1, value2, ..., sep=' ', end='\n')
Here,
vlaue1, value2 are the value(s) to be printed.
sep separator is used between the values (default value is space).
After all values are printed, end is printed. (default value is \n).
Input/Output Functions:
Single, Double and Triple Quoted Strings:
Single Quoted Strings:
In Python, single-quoted strings are enclosed within single quotes (‘ ‘).
They are used to store text, and single-quoted strings are especially useful
when the text contains double quotes inside.
For example,
‘ He said, “ Python is fun!” ‘
Single, Double and Triple Quoted Strings:
Single, Double and Triple Quoted Strings:
Double Quoted Strings:
Double-quoted strings are enclosed within double quotes (" ").
They are like single-quoted strings but are convenient when the text
contains single quotes.
For example,
“ It’s a sunny day ”
Single, Double and Triple Quoted Strings:
Single, Double and Triple Quoted Strings:
Triple Quoted Strings:
Triple-quoted strings are enclosed in triple single quotes (''' ''') or triple
double quotes (""" """).
They are used for multi-line strings and can include both single and
double quotes.
For example, """ She said, "It's time to learn Python!" """
"""Python is easy to learn,
powerful, and versatile."""
Single, Double and Triple Quoted Strings:
Single, Double and Triple Quoted Strings:
Objects and Dynamic Typing: Object
In Python, everything is treated as an object, including numbers, strings,
lists, functions, and even classes.
An object represents a value stored in memory and has an identity, a
type, and associated methods.
When a variable is created, the variable does not store the value directly;
instead, it references an object.
For example, when we write x = 10, the variable x refers to an integer
object with value 10.
Objects and Dynamic Typing:
Python allows a variable to change its type during program execution
without explicit declaration.
Objects and Dynamic Typing:
Python allows a variable to change its type during program execution
without explicit declaration.
Operators:
Python supports the following operators.
o Arithmetic Operators
o Comparison (Relational) Operators
o Assignment Operators
o Logical Operators
o Bitwise Operators
o Membership Operators
o Identity Operators
Arithmetic Operators:
Arithmetic Operators:
Relational Operators: p = 10, q = 20
Relational Operators:
Assignment Operators:
Logical Operators:
Logical Operators:
Bitwise Operators:
Bitwise Operators:
Bitwise Operators:
Bitwise Operators:
Bitwise Operators:
Membership Operators:
Membership operators are used to test whether a value is present in a
sequence (or) collection such as a list, tuple, string, set, or dictionary.
They return a Boolean result (True or False).
The following are two membership operators in Python.
o in
o not in
Membership Operators:
The in operator returns True if the specified value is found in the
sequence; otherwise, it returns False.
The not in operator returns True if the specified value is not present in
the sequence; otherwise, it returns False.
Identity Operators:
Identity operators are used to check whether two variables refer to the
same object in memory.
not whether they have the same value.
Python provides two identity operators:
o is
o is not
Identity Operators:
The is operator returns True if both variables point to the same object in
memory. Otherwise; it returns False.
The is not operator returns True if the variables point to different objects
in memory. Otherwise; it returns False.
Identity Operators:
Identity Operators:
Operator Precedence and Associativity:
Operator precedence determines which operator is evaluated first when
an expression has more than one operator.
The operator with higher precedence or priority is evaluated first and the
operator with the least priority is evaluated later.
Associativity decides the order of evaluation when two or more operators
of the same precedence appear in an expression.
Operator Precedence and Associativity:
Control Statements:
Selection Statements: if
Syntax:
if condition:
Statement 1
Statement 2
Statement 3
Statement 4
Selection Statements: if…else
Syntax:
if condition:
Statement 1
Statement 2…
else:
Statement 3
Statement 4…
Statement 5
Selection Statements: if…elif…else
Syntax:
if condition1:
Statement 1…
elif condition2:
Statement 2…
elif condition3:
Statement 3…
else:
Statement 4…
Statement 5
Selection Statements: Nested if…else
Syntax:
if condition1:
if condition2:
Statement 1…
else: if Block
Statement 2…
else:
Statement 3… else Block
Statement 4
Iterative Statements:
An Iterative statement allows us to execute a statement or group of
statements multiple times.
In Python Iteration (Loops) statements are of three types:-
o While Loop.
o for Loop.
o Nested For Loops.
Iterative Statements: while loop
Syntax:
While condition:
Statement 1
Statement 2
.
.
.
Statement n
Statement n+1
Iterative Statements: for loop
A for loop in Python is used to iterate over a sequence such as a list,
tuple, string, or range and execute a block of code repeatedly for each
element in the sequence.
Syntax:
for variable in sequence:
Statement 1
Statement 2…
Statement n+1
Iterative Statements: range()
The range() function is used to generate a sequence of numbers.
It is commonly used with for loops to control the number of iterations.
Syntax:
range(start, stop, step)
Here, start → Starting value (default = 0)
stop → Ending value (excluding)
step → Difference between numbers (default = 1)
Iterative Statements: range()
Iterative Statements: Nested for loop
Syntax:
for variable1 in sequence1:
for variable2 in sequence2:
statements(s)
Iterative Statements: Nested for loop
Iterative Statements: Nested for loop
Jump Statements:
Jump statements are used to control the flow of execution by
transferring control from one part of a program to another.
They are mainly used inside loops to skip statements, exit loops, or do
nothing.
Python provides three jump statements:
o break
o continue
o pass
Jump Statements: break
The break statement is used to terminate a loop immediately, even if the
loop condition is still true.
When break is executed, control moves out of the loop to the next
statement after the loop.
Jump Statements: break
Jump Statements: break
Jump Statements: continue
The continue statement is used to skip the current iteration of the loop
and move to the next iteration.
The loop does not terminate; only the remaining statements in the current
iteration are skipped.
Jump Statements: continue
Jump Statements: continue
Jump Statements: pass
The pass statement does nothing.
It is used as a placeholder when a statement is syntactically required but
no action is needed at that moment.