Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

UNIT 2 Python Part1

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

UNIT 2 Python Part1

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

UNIT 2:

PYTHON PROGRAM FLOW


CONTROL CONDITIONAL
BLOCKS
SUBJECT :PYTHON
PROGRAMMING
input() Function in Python
The input function in Python is a powerful tool for interacting with users
and collecting data from the keyboard during program execution.

The input function in python reads the input as a string, which can then
be converted into other data types, such as integers, floating-point
numbers, or booleans.

Syntax of Input Function in Python : input([prompt])


Example 1 , of input() Function in Python: taking the Name and ID of
the user as the input from the user and printing it.
Example 2 :Taking two numbers from the
users as input and then printing the
multiplication of those numbers.
• Converting Input into other Data Types
To convert the input into other data types, such as integers or floating-point
numbers, we can use the built-in conversion functions, such as int(), float(), or
bool().

• Handling Exceptions
When converting the input into other data types, it is essential to handle
exceptions. If the input provided by the user cannot be converted into the
desired data type, it will cause an error.

• Default Value
The input function also allows us to provide a default value, which will be
used if the user does not provide any input.
Conditional Statements in
Python
• Conditional Statements are statements in Python that provide a
choice for the control flow based on a condition.
• It means that the control flow of the Python program will be
decided based on the outcome of the condition.
Types of Conditional Statements in Python
1. If Conditional Statement in Python
2. If else Conditional Statements in Python
3. Nested if..else Conditional Statements in Python
4. If-elif-else Conditional Statements in Python
5. Ternary Expression Conditional Statements in Python
If Conditional Statement in
Python

• The if-else statement in Python is used to execute a block of code


when the condition in the if statement is true, and another block of
code when the condition is false.
Flowchart of If Statement :-
Syntax of If Statement:

if condition:
# Statements to execute if
# condition is true

# if statement example
if 10 > 5:
print("10 greater than 5")

print("Program ended")
Python supports the standard mathematical logical
conditions:
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
These conditions can be utilized in a variety of ways in the if
statement.
Practice Question :Program using
‘if’ statement
1. Python program to illustrate if the number is even using if
statement
2. Program to print the largest of the three numbers.
1.
2.
The if-else statement
• The if-else statement provides an else block combined with
the if statement which is executed in the false case of the
condition.

• If the condition is true, then the if-block is executed.


Otherwise, the else-block is executed.
The syntax of the if-else
statement is given below

if condition:
#block of statements
else:
#another block of statements
(else-block)
Practice questions : ‘if-else’
statement
1. Program to check whether a person is eligible to vote or not.
2. Program to check whether a number is even or not.
1.
2.
The elif statement
• The elif statement enables us to check multiple conditions and
execute the specific block of statements depending upon the true
condition among them.

• We can have any number of elif statements in our program depending


upon our need. However, using elif is optional.

• The elif statement works like an if-else-if ladder statement in C. It


must be succeeded by an if statement
• The syntax of the elif statement is given below:-
if expression 1:
# block of statements

elif expression 2:
# block of statements

elif expression 3:
# block of statements

else:
# block of statements
One line if statement:
if a > b: print("a is greater than b“)

One line if else statement:


a=2
b = 330
print("A") if a > b else print("B")

One line if else statement, with 3 conditions:


a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
Practice Question: elif statement
1. Write a program that takes user marks obtained in 5 subjects as
input and print their grade based on criteria (A,B,C,D,E)
2. Write a program to check whether a given year is a Leap Year.
• If a year is evenly divisible by 4 means having no remainder then go to next step.
If it is not divisible by 4. It is not a leap year. For example: 1997 is not a leap year.
• If a year is divisible by 4, but not by 100. For example: 2012, it is a leap year. If a
year is divisible by both 4 and 100, go to next step.
• If a year is divisible by 100, but not by 400. For example: 1900, then it is not a
leap year. If a year is divisible by both, then it is a leap year. So 2000 is a leap year

3. Python Program to check whether a number is positive or negative.


• 1.
• 3.


Python For Loops
• A 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.

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
Lets examine the basic structure of a for loop in python :

animal = [“cat” ,”dog”,”penguin”,”whale”,”zebra”]

For x in animal :
print(x)
# you can understand this as ; for every animal ‘x’ in the list ‘animal’ ,print
the animal ‘x’.
#’x’ is known as the iterator and ‘animals’ is known as the iterable
Output :- cat
dog
penguin
whale
zebra **for and in are both Python keywords**
How to write a for loop in Python
Step 1
Tell Python you want to create a for loop by starting the statement with
for.
Step 2
Write the iterator variable (or loop variable). The iterator takes on each
value in an iterable (for example a list, tuple, or range) in a for loop one
at a time during each iteration of the loop.
**iterable means an object that can be looped over.
String Iteration

or
Practice Program :
1. Write a for loop that prints the numbers from 1 to 10, inclusive.
(Hint: There's more than one way to do this)

2. Suppose you have a list called box_of_kittens


['fluffy','whiskers','mittens','socks'] as your iterable. You could name
your iterator variable anything you want, but you might choose to
call it 'kitten' to reference that you'll be looping through each
individual kitten [😺] in box_of_kittens.
• 2.
for Loop with Python range()
• In Python, the range() function returns a sequence of numbers. For
example,
Here, range(4) returns a sequence of 0, 1, 2 ,and 3.
Since the range() function returns a sequence of numbers, we can
iterate over it using a for loop. For example:-

o/p =
PROGRAMS FOR PRACTICE :
• Python program to calculate the sum of all numbers from 1 to a given
number.
• Python program to calculate the sum of all the odd numbers within
the given range.
• Python program to print a multiplication table of a given number.
• Python program to display numbers from a list using a for loop.
• Python program to check if the given string is a palindrome.
• Python program to check if a given number is an Armstrong number
• Python program to check whether a given num is prime number or
not.
• Python program to get the Fibonacci series between 0 to 50
• Tip: We can end a for loop before iterating through all the items by
using a break statement.
The continue Statement
With the continue statement we can stop the current iteration of the
loop, and continue with the next:
Example
Do not print banana:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)

You might also like