[Go to site: main page, start]

0% found this document useful (0 votes)
4 views9 pages

Chet Python Assignment

The document provides an overview of operators, control flow statements, functions, and loops in Python. It explains various types of operators including arithmetic, assignment, comparison, logical, and bitwise operators, as well as control flow statements like if, if-else, and if-elif-else. Additionally, it covers the syntax and usage of for and while loops, along with examples of functions for checking even or odd numbers and palindromes.

Uploaded by

chethana3036
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Chet Python Assignment

The document provides an overview of operators, control flow statements, functions, and loops in Python. It explains various types of operators including arithmetic, assignment, comparison, logical, and bitwise operators, as well as control flow statements like if, if-else, and if-elif-else. Additionally, it covers the syntax and usage of for and while loops, along with examples of functions for checking even or odd numbers and palindromes.

Uploaded by

chethana3036
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1a) Operators are symbols, such as +, –, =, >, and >> 4 + 6

where 4 and 6 are operands and + is the operator. Python


language supports a wide range of operators.
They are 1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
Arithmetic Operators :
Arithmetic operators are used to execute arithmetic
operations such as addition, sub traction, division,
multiplication etc.
For example, 1. >>> 10+35 45
2. >>> −10+35

Assignment operators :
are used for assigning the values generated after evaluating
the right operand to the left operand. Assignment operation
always works from right to left. Assignment operators are
either simple assignment operator or compound assignment
operators.
For example, 1. >>> x = 5 2. >>> x = x + 1

Comparison Operators:
When the values of two operands are to be compared then
comparison operators are used. The output of these
comparison operators is always a Boolean value, either True
or False.
For example,
1. >>>10 == 12 False
2. >>>10 != 12 True

Logical operators:
The logical operators are used for comparing or negating the
logical values of their oper ands and to return the resulting
logical value. The values of the operands on which the logical
operators operate evaluate to either True or False. The result
of the logical operator is always a Boolean value, True or
False.
1. >>> True and False False
2. >>> True or False True

Bitwise operators:
treat their operands as a sequence of bits (zeroes and ones)
and perform bit by bit operation.
1. >>> p =60
2. >>> p << 2 240
1b) If control flow statement:
The syntax for if statement is:
if Boolean_ Expression:
statement(s)
The if decision control flow statement starts with if keyword
and ends with a colon. The expression in an if statement
should be a Boolean expression. The if statement decides
whether to run some particular statement or not depending
upon the value of the Boolean expression. If the Boolean
expression evaluates to True then statements in the if block
will be executed; otherwise the result is False then none of
the statements are executed
Eg:
>>> if 20 > 10:
print(f"20 is greater than 10")
Output:20 is greater than 10

If-else control flow statements:


An if statement can also be followed by an else statement
which is optional. An else statement does not have any
condition. Statements in the if block are executed if the
Boolean_Expression is True. Use the optional else block to
execute statements if the Boolean_Expression is False. The
if…else statement allows for a two-way decision.
The syntax for if…else statement is,
if Boolean_Expression:
statement_1
else: statement_2
eg: Program to Find If a Given Number Is Odd or Even
number = int(input("Enter a number"))
if number % 2 == 0:
print(f"{number} is Even number")
else:
print(f"{number} is Odd number")
Output:
Enter a number: 45
45 is Odd number

If-elif-else control flow statement:


The if…elif…else is also called as multi-way decision control
statement. When you need to choose from several possible
alternatives, then an elif statement is used along with an if
statement. The keyword ‘elif’ is short for ‘else if’ and is useful
to avoid excessive indenta tion. The else statement must
always come last, and will again act as the default action. The
syntax for if…elif…else statement is,
if Boolean_Expression_1:
statement_1
elif Boolean_Expression_2:
statement_2
elif Boolean_Expression_3:
statement_3
else: statement_last

Eg: Program to Display the Cost of Each Type of Fruit


fruit_type = input("Enter the Fruit Type:")
if fruit_type == "Oranges":
print('Oranges are $0.59 a pound')
elif fruit_type == "Apples":
print('Apples are $0.32 a pound')
elif fruit_type == "Bananas":
print('Bananas are $0.48 a pound')
elif fruit_type == "Cherries":
print('Cherries are $3.00 a pound')
else:
print(f'Sorry, we are out of {fruit_type}')
Output:Enter the Fruit Type: Cherries
Cherries are $3.00 a pound
2a) Functions are one of the fundamental building blocks in
Python programming language. Functions are used when you
have a block of statements that needs to be executed mul
tiple times within the program. Functions can be either Built-
in Functions or User-defined functions.

def is_even(number):
"""Check if a number is even."""
return number % 2 == 0

def check_even_or_odd():
try:
num = int(input("Enter a number: "))
if is_even(num):
print(f"{num} is even.")
else:
print(f"{num} is odd.")
except ValueError:
print("Invalid input. Please enter an integer.")

check_even_or_odd()
2b) For loop:
The syntax for the for loop is,
for iteration_variable in sequence:
Indentation statement(s)
Colon should be present at the end Keyword The for loop
starts with for keyword and ends with a colon. The first item
in the sequence gets assigned to the iteration variable
iteration_variable. Here, iteration_variable can be any valid
variable name. Then the statement block is executed. This
process of assigning items from the sequence to the
iteration_variable and then executing the statement
continues until all the items in the sequence are completed.

Program to Iterate through Each Character in the String Using


for Loop 1. for each_character in "Blue": 2. print(f"Iterate
through character {each_character} in the string 'Blue'")
Output Iterate through character B in the string 'Blue' Iterate
through character l in the string 'Blue' Iterate through
character u in the string 'Blue' Iterate through character e in
the string 'Blue'

While loop:
The syntax for while loop is,
while Boolean_Expression:
Indentation statement(s)
Colon should be present at the end The while loop starts with
the while keyword and ends with a colon. With a while state
ment, the first thing that happens is that the Boolean
expression is evaluated before the statements in the while
loop block is executed. If the Boolean expression evaluates to
False, then the statements in the while loop block are never
executed. If the Boolean expression evaluates to True, then
the while loop block is executed. After each iteration of the
loop block, the Boolean expression is again checked, and if it
is True, the loop is iterated again.
Write Python Program to Display First 10 Numbers Using
while Loop Starting from 0
i=0
while i < 10:
print(f"Current value of i is {i}")
i=i+1
Output : Current value of i is 0
Current value of i is 1
Current value of i is 2
Current value of i is 3
Current value of i is 4
Current value of i is 5
Current value of i is 6
Current value of i is 7
Current value of i is 8
Current value of i is 9
2c)
def is_palindrome(s):
s = [Link]().replace(" ", "") # Optional: ignore case and
spaces
return s == s[::-1]

def main():
user_input = input("Enter a string: ")
if is_palindrome(user_input):
print(f"'{user_input}' is a palindrome.")
else:
print(f"'{user_input}' is not a palindrome.")

main()

You might also like