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

py_ut2

The document outlines various Python programming concepts including membership operators, comparison operators, and the use of the 'elif' keyword for conditional statements. It also provides examples of loops, functions for checking palindromes and calculating factorials, and explains identity and assignment operators. Additionally, it covers decision-making statements and includes code snippets for practical understanding.

Uploaded by

Vaibhavi Shinde
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 views11 pages

py_ut2

The document outlines various Python programming concepts including membership operators, comparison operators, and the use of the 'elif' keyword for conditional statements. It also provides examples of loops, functions for checking palindromes and calculating factorials, and explains identity and assignment operators. Additionally, it covers decision-making statements and includes code snippets for practical understanding.

Uploaded by

Vaibhavi Shinde
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
You are on page 1/ 11

Unit- 2

1) Give membership operators in python.


Python has two membership operators:
 in
 not in
2) List comparison operators in Python.

3) Write the use of elif keyword in python.


In Python, the elif keyword stands for "else if,"
The elif statement enables us to check multiple conditions and execute the specific
block of statements depending upon the true condition among them.
It allows you to test additional conditions if the previous if or elif conditions were not
true.
Syntax:
if condition1:
# Block of code if condition1 is true
elif condition2:
# Block of code if condition2 is true
elif condition3:
# Block of code if condition3 is true
else:
# Block of code if none of the conditions are true

Example:
age = 25

if age < 18:


print("You are a minor.")
elif age >= 18 and age < 65:
print("You are an adult.")
else:
print("You are a senior citizen.")

4) Print the following pattern using loop:


rows = 4 # Number of rows
num = 7 # Starting number of characters in the first row

for i in range(rows):
spaces = " " * i # Add leading spaces
ones_zeros = "".join("1" if j % 2 == 0 else "0" for j in range(num -
2 * i))
print(spaces + ones_zeros)

5) Write a python program that takes a number and checks whether it is a palindrome.
num = int(input("Enter a value:"))
temp = num
rev = 0
while(num > 0):
dig = num % 10
rev = rev * 10 + dig
num = num // 10
if(temp == rev):
print("This value is a palindrome number!")
else:
print("This value is not a palindrome number!")

Output :
Enter a value:121
This value is a palindrome number!

6) Write a python program takes in a number and find the sum of digits in a number
num = input("Enter Number: ")
sum = 0

for i in num:
sum = sum + int(i)

print(sum)
Output:
Enter Number: 123
6

7) Explain membership and Identity operators in Python.

Identity operators are used to compare the memory locations of two objects. There are two
identity operators:

is:This operator checks if two objects refer to the same memory location (i.e., they are the
same object). It returns True if both variables point to the same object.

Example:

# Using 'is' operator


a = [1, 2, 3]
b=a
print(a is b) # True, because both 'a' and 'b' refer to the same object in memory.

c = [1, 2, 3]
print(a is c) # False, because 'a' and 'c' refer to different objects in memory.

is not: This operator checks if two objects do not refer to the same memory location. It returns
True if they are not the same object and False if they are the same object.

Example:

# Using 'is not' operator


a = [1, 2, 3]
b = [1, 2, 3]
print(a is not b) # True, because 'a' and 'b' are different objects, even though their
contents are the same.

8) Write python program to display output like.

pattern = [[2], [4, 6, 8], [10, 12, 14, 16, 18]]


for row in pattern:
for num in row:
print(num, end=" ")
print() # Move to the next line after printing a row

9) Explain use of Pass and Else keyword with for loops in python.

10) Describe Keyword "continue" with example.


In Python, the continue keyword is used inside loops (such as for or while) to skip the
current iteration and move to the next iteration of the loop. When continue is
encountered, the rest of the code inside the loop is skipped for that particular iteration,
and the loop proceeds to the next iteration.

Example:

Output:
1
2
4
5

11) Write a Python program to find the factorial of a number provided by the user.

num = int(input("Enter a number: "))

# Check if the input number is negative


if num < 0:
print("Factorial does not exist for negative numbers.")
else:
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}")

Enter a number: 5
The factorial of 5 is 120

12) Explain Bitwise operator in Python with appropriate example.


Bitwise operators acts on bits and performs bit by bit operation. Assume a=10 (1010) and b=4
(0100)
13) List identity operators in python
Identity operators in Python are
• is
• is not
14) Write a program to print following:

for i in range(1,5):
for j in range(1,i+1):
print(j,end=' ')
print("")

15) Explain membership and assignment operators with example.


Membership Operators: The membership operators in Python are used to find the existence of a
particular element in the sequence, and used only with sequences like string, tuple, list,
dictionary etc.
Membership operators are used to check an item or an element that is part of a string, a list or a
tuple.
A membership operator reduces the effort of searching an element in the list. Python provides
‘in’ and ‘not in’ operators which are called membership operators and used to test whether a
value or variable is in a sequence.

Assignment Operators (Augmented Assignment Operators): Assignment operators are used in


Python programming to assign values to variables.
The assignment operator is used to store the value on the right-hand side of the expression on the
left-hand side variable in the expression.

For example, a = 5 is a simple assignment operator that assigns the value 5 on the right to the
variable a on the left.

There are various compound operators in Python like a += 5 that adds to the variable and later
assigns the same. It is equivalent to a = a + 5. Following table shows assignment operators in Python
programming:
16) Explain decision making statements If- else, if- elif- else with example.
The if-else statement: if statements executes when the conditions following if is true and it does
nothing when the condition is false. The if-else statement takes care of a true as well as false
condition.

if-elif-else (ladder) statements: Here, a user can decide among multiple options. The if statements are
executed from the top down. As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is
true, then the final else statement will be executed.

You might also like