UNIT 2 Python Part1
UNIT 2 Python Part1
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.
• 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
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 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.
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“)
•
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.
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)
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)