Python unit-2 notes (1)
Python unit-2 notes (1)
FLOWCHART:
Example-1:
Q.What message will be printed if the temperature is 25 degrees Celsius?
temperature = 25
if temperature >50:
print("It is hot outside.")
elif temperature >30:
print("It is a normal day.")
else:
print("It is cool outside.")
=>Since the temperature variable (25) does not satisfy the conditions of the if
or elif statements, the code will execute the else block. Therefore, the output
will be.
It is cool outside.
example-2:
Q.2 Create a simple authentication system in Python where the user is asked
to enter a username and password. If the username is "admin" and the
password is "password123", print "Access granted." Otherwise, print "Access
denied."?
ANS: # Define the correct username and password
correct_username = "admin"
correct_password = "password123"
username = input("Enter your username: ")
password = input("Enter your password: ")
# Check if the entered username and password match the correct ones
If username == correct_username and password == correct_password:
print("Access granted.")
else:
print("Access denied.")
nested if statements:
you can use nested if statements to create multiple levels of conditional
statements. Nested if statements allow you to check conditions within
conditions. You can have as many levels of nesting as needed, but it's
important to maintain proper indentation to indicate the level of nesting.
Example-1
Q. Write a java program that uses nested if statements to check if x is greater
than 5 and if y is greater than 2. If both conditions are met, print 'x is greater
than 5, and y is also greater than 2.' If x is not greater than 5, print 'x is not
greater than 5.?
x = 10
y=5
if x > 5:
print("x is greater than 5")
if y > 2:
print("y is also greater than 2")
else:
print("y is not greater than 2")
else:
print("x is not greater than 5")
# Output:
# x is greater than 5
# y is also greater than 2
Example -2
Q.Write a Python program that checks whether a person is eligible for a loan
or not based on two variables: age and income. If the age is 18 or older and
the income is 30000 or higher, the program should print 'You are eligible for a
loan.' Otherwise, it should print 'You are not eligible for a loan.' Provide the
code for this program.
age = 25
income = 50000
if age >= 18:
if income >= 30000:
print("You are eligible for a loan.")
else:
print("Your income is too low for a loan.")
else:
print("You are underage for a loan.")
# Output:# You are eligible for a loan.
for Loop:
Example:
Flowchart definition:
A flowchart is a visual representation of a process or system using
symbols and connecting lines. It helps illustrate the sequence of steps,
decisions, and inputs in a clear and organized manner.
Example:
count = 1
while count <= 5:
print(count)
count += 1
o/p
1
2
3
4
5
Question: Write a Python program (WAP) to print the numbers from 1 to 20 using a loop.
Please provide the code for your program.
# binary to decimal
num=int(input("enter a number"))
sum=0
i=0
while num!=0:
rem=num%10
sum=sum+rem*(2**i)
i=i+1
num=num//10
print("decimal number is ",sum)
Question: Write a Python program (WAP) to convert a decimal number to its binary
equivalent. The program should accept a decimal number as input ?
# decimal to binary
num=int(input("enter a decimal number"))
sum=0
i=0
while num!=0:
rem=num%2
sum=sum+rem*(10**i)
i=i+1
num=num//2
print("binary number is ",sum)
nested loops:
In Python, you can create nested loops by placing one loop inside another.
Nested loops are often used for tasks that require iterating through multiple
dimensions, such as two-dimensional arrays or nested data structures.
Example:
Q. W.A.P to print this pattern in python?
***
***
***
for i in range(0,3):
for j in range(0,3):
print("*", end=" ")
print()
**
***
for i in range(0,3):
for j in range(0,i):
print("*", end=" ")
print()
break statement:
• The break statement is used to exit the current loop prematurely.
• When a break statement is encountered, it immediately terminates the
loop, and control is transferred to the next statement after the loop.
• It is often used when you want to stop a loop early based on a certain
condition.
Example of break:
for i in range(10):
if i == 5:
break
print(i)
In this example, the loop will terminate when i becomes 5, and the
numbers 0 to 4 will be printed.
continue statement:
•The continue statement is used to skip the rest of the current
iteration and move to the next iteration of the loop.
• When a continue statement is encountered, it skips the remaining
code within the current iteration and starts the next iteration of
the loop.
• It is often used to bypass certain iterations based on specific
conditions.
Example of continue:
for i in range(10):
if i % 2 == 0:
continue
print(i)
o/p
1
3
5
7
9
In this example, the continue statement is used to skip even numbers, so
only odd numbers from 1 to 9 will be printed.
Both break and continue are powerful tools for controlling the flow of
loops in Python and can be used to make your code more efficient and
flexible
while True:
user_input = input("Enter a number or 'q' to quit ")
if user_input == 'q':
break # Exit the loop if the user enters 'q'
number = int(user_input)
print(“your number was ”,number)
In this example, the break statement is used to exit the while loop when
the user enters 'q'.
in this example, the break statement is used to exit the inner loop when
j is equal to 3. The outer loop continues running.
pass statement
in Python, the pass statement is a null operation or a no-op. It does
nothing when it is executed. You can use the pass statement as a
placeholder when you need a statement syntactically but don't want to
perform any action. This can be particularly useful in situations where
you are designing code structure or leaving a function, class, or loop
body empty for later implementation.
Empty Loop: You can use pass to create a loop that doesn't execute any
specific code but is used as a placeholder for a loop body.
for item in some_list:
pass # Placeholder, no action
Empty Function: You can use pass to define a function that doesn't
perform any action initially, but you might plan to add code to it later.
def some_function():
pass # Placeholder for future code
Empty Class: Similarly, you can use pass in a class definition when you
are creating the structure of a class but don't have the implementation
details yet
class SomeClass:
pass # Placeholder for class structure
-
1
22
333
4444
################################################################
n=5
for i in range(0,n):
for j in range(0,i):
print(j,end="")
print()
0
01
012
0123
################################################################
################################################################
n=5
for i in range(0,n):
for j in range(0,n-i):
print(" ",end="")
for j in range(0,i):
print("*",end="")
print()
*
**
***
n=5
for i in range(0,n):
for j in range(0,n-i):
print(" ",end="")
for j in range(0,2*i-1):
print("*",end="")
print()
*
***
*****
n=5
for i in range(0,n):
for j in range(0,n-i):
print(" ",end="")
for j in range(0,2*i-1):
print(j,end="")
print()
0
012
01234
0123456
################################################################
n=5
c=65
for i in range(0,n):
for j in range(0,n):
print(chr(c),end="")
c=c+1
print()
print(" Z")
ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z
################################################################
n=5
c=65
for i in range(0,n):
for j in range(0,n):
print(chr(c),end="")
c=c+1
print()
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
################################################################
for i in range(65,91):
print(chr(i),end="")
ABCDEFGHIJKLMNOPQRSTUVWXYZ
################################################################
n=7
for i in range(1,n):
print("PASS",i," - ",end="")
for j in range(1,n):
print(j,end=" ")
print()
PASS 1 - 1 2 3 4 5 6
PASS 2 - 1 2 3 4 5 6
PASS 3 - 1 2 3 4 5 6
PASS 4 - 1 2 3 4 5 6
PASS 5 - 1 2 3 4 5 6
PASS 6 - 1 2 3 4 5 6
################################################################
1
121
12321
1234321
n=6
for i in range(1,n):
for j in range(1,n-i):
print(" ",end="")
for j in range(1,i):
print(j,end="")
for j in range(i-2,0,-1):
print(j,end="")
print()
Problems on loops
Q.W.A.P TO PRINT THIS?
0123
n=4
for i in range(0,n,1):
print(i,end=" ")
################################################################
0 2 4 6 8 10 12 14
n=15
for i in range(0,n,2):
print(i,end=" ")
################################################################
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
n=15
for i in range(n,0,-1):
print(i,end=" ")
################################################################
15 13 11 9 7 5 3 1
n=15
for i in range(n,0,-2):
print(i,end=" ")
################################################################
Q. "W.A.P in Python to calculate the sum of natural numbers up to a given
value 'n' ?