py_ut2
py_ut2
Example:
age = 25
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
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:
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:
9) Explain use of Pass and Else keyword with for loops in python.
Example:
Output:
1
2
4
5
11) Write a Python program to find the factorial of a number provided by the user.
Enter a number: 5
The factorial of 5 is 120
for i in range(1,5):
for j in range(1,i+1):
print(j,end=' ')
print("")
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.