Python Ppt for Chapter 1
Python Ppt for Chapter 1
More Shekhar
Chapter 1
Fundamentals of
Python
Introduction
• What is Python?
• Python is a popular programming language. It was
created by Guido van Rossum, and released in
1991.
It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.
Features of python:
• Easy-to-learn: Python has few keywords, simple structure, and a clearly defined
syntax. This allows a student to pick up the language quickly.
• Easy-to-read: Python code is more clearly defined and visible to the eyes.
• Easy-to-maintain: Python's source code is fairly easy-to-maintain.
• A broad standard library: Python's bulk of the library is very portable and
Cross platform compatible on UNIX, Windows, and Macintosh.
• Interactive Mode: Python has support for an interactive mode, which allows
interactive testing and debugging of snippets of code.
• Portable: Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
• Extendable: You can add low-level modules to the Python interpreter. These modules
enable programmers to add to or customize their tools to be more efficient.
• Databases: Python provides interfaces to all major commercial databases.
• GUI Programming: Python supports GUI applications that can be created and ported
What can Python do?
# Defining variables
x = 10
y = "Hello, World!"
z = True
# Defining a function
def greet(name):
print("Hello, " + name + "!")
• Valid identifiers:
• var1
• _var1
• _1_var
• var_1
Invalid Identifiers
• !var1
• 1var
• 1_var
• var#1
• var 1
Literals in Python
Boolean Literal Represents two truth values: True and True, False
False
List, Dict, Set, Tuple Literals Compound data types with their [1, 2], {"key":"value"}, {1,2,3}, (1,2)
specific literals
PYTHON Data Types
• There are different types of data types in Python.
Some built-in Python data types are:
• Numeric data types: int, float, complex
• String data types: str
• Sequence types: list, tuple, range
• Binary types: bytes, byte array, memory view
• Mapping data type: dict
• Boolean type: bool
• Set data types: set, frozen set
Python Numeric Data Type
Output:
set
EXAMPLE
thisset = {"apple", "banana", "cherry"}
print(thisset)
print(thisset)
OUTPUT
• {'banana', 'apple', 'cherry'} // FIRST O/P
# Note: the set list is unordered, meaning: the items
will appear in a random order.
.
Understanding Python blocks
• Python is a high-level, interpreted
programming language that is easy to learn
and use. It has a simple and easy-to-
understand syntax that emphasizes readability
and reduces the cost of program maintenance.
The basic structure of a Python program
consists of the following components:
Structure of Python
• Comments: Comments are used to explain the purpose of the code or to make notes for
other programmers. They start with a ‘#’ symbol and are ignored by the interpreter.
• Import Statements: Import statements are used to import modules or libraries into the
program. These modules contain predefined functions that can be used to accomplish
tasks.
• Variables: Variables are used to store data in memory for later use. In Python, variables do
not need to be declared with a specific type.
• Data Types: Python supports several built-in data types including integers, floats, strings,
booleans, and lists.
• Operators: Operators are used to perform operations on variables and data. Python
supports arithmetic, comparison, and logical operators.
• Control Structures: Control structures are used to control the flow of a program. Python
supports if-else statements, for loops, and while loops.
• Functions: Functions are used to group a set of related statements together and give them a
name. They can be reused throughout a program.
• Classes: Classes are used to define objects that have specific attributes and methods. They
are used to create more complex data structures and encapsulate code.
• Exceptions: Exceptions are used to handle errors that may occur during the execution of a
program.
EXAMPLE
• # This is a comment
# Defining variables
x = 10
y = "Hello, World!"
z = True
# Defining a function
def greet(name):
print("Hello, " + name + "!")
The if statement is the most simple decision-making statement. It is used to decide whether a
• # if syntax Python
if condition:
# Statements to execute if
# condition is true
EXAMPLE
• # python program to illustrate If statement
i = 10
if (i > 15):
print("10 is less than 15")
print("I am Not in if")
Output-
I am Not in if
Python If Else Statement
• if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
EXAMPLE
• # python program to illustrate else if in Python
statement
i = 20
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
OUTPUT
i is greater than 15
I'm in else Block
I'm not in if and not in else Block
Python Nested If Statement
1)While
2) for
3)Continue
4) break
For loop
for loop is used for iterating over a sequence
(that is either a list, a tuple, a dictionary, a set,
or a string).
With the for loop we can execute a set of
statements, once for each item in a list, tuple,
set etc.
EXAMPLE
• Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
OUTPUT:-
apple
banana
cherry
While loop
In Python, a while loop is used to execute a
block of statements repeatedly until a given
condition is satisfied. When the condition
becomes false, the line immediately after the
loop in the program is executed.
• Python While Loop Syntax:
while expression:
statement(s)
EXAMPLE
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")
OUTPUT:-
Hello Geek
Hello Geek
Hello Geek
The continue Statement
OUTPUT:-
Current name is : alex
Current name is : brian
Current name is : charles
2. Python for Loop with Tuple
OUTPUT:-
Current item is item1
Current item is item1
Current item is item3
3. Python for Loop with Dictionary
OUTPUT:-
0
1
2
3
4
Comprehensions on List, Tuple,
Dictionaries
Python List Comprehension
Output
• The sum of 1.5 and 6.3 is 7.8
Python Program to Find the Square Root
num1 = 10
num2 = 14
num3 = 12
take three numbers from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
o/p- The largest number is 14