|
| 1 | +# ---------FOR LOOP--------------- |
| 2 | +# The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. |
| 3 | +for i in range(10): |
| 4 | + print(i) |
| 5 | +#print table using for loop |
| 6 | +number= int(input("Enter the number")) |
| 7 | +for count in range(1,11): |
| 8 | + table= number*count |
| 9 | + print(number, "x" , count, "=", table) |
| 10 | +# #---------------------------------------- |
| 11 | +for i in range(1,50): |
| 12 | + print(i) |
| 13 | + |
| 14 | +# --------WHILE LOOP------------------- |
| 15 | +#Python while loop: The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true. |
| 16 | +count=0 |
| 17 | +while count<=4: |
| 18 | + print("hello, im a while loop") #print unfinite bcz not given stop... |
| 19 | + count= count+1 |
| 20 | + |
| 21 | +# #print table using while loop |
| 22 | +number= int(input("Enter the number")) |
| 23 | +count=1 |
| 24 | +while count<=10: |
| 25 | + table= number*count |
| 26 | + print(number, "x", count, "=", table) |
| 27 | + count = count+1 |
| 28 | + |
| 29 | +#print table in reverse using while loop |
| 30 | +number= int(input("Enter the number")) |
| 31 | +count=10 |
| 32 | +while count>=1: |
| 33 | + table= number*count |
| 34 | + print(number, "x", count, "=", table) |
| 35 | + count = count-1 |
| 36 | + |
| 37 | +# Python break and continue Statement: |
| 38 | +for i in range(1,6): |
| 39 | + print(i) |
| 40 | + |
| 41 | +for i in range(1,6): |
| 42 | + print(i) |
| 43 | + break |
| 44 | + |
| 45 | +for i in range(1,5): |
| 46 | + if i==3: |
| 47 | + break |
| 48 | + print(i) |
| 49 | +print("the end") |
| 50 | + |
| 51 | +for i in range(5): |
| 52 | + number= float(input("enter the number: ")) |
| 53 | + if number<0: |
| 54 | + continue |
| 55 | + print(number) |
| 56 | + |
| 57 | +#Pass Statement: pass statement is a null statement. |
| 58 | +number=5 |
| 59 | +if number>0: |
| 60 | + pass |
| 61 | + |
| 62 | + |
| 63 | +#---------FUNCTION------------ |
| 64 | +# Functions: A function is a group of related statements that performs a specific task. |
| 65 | +# * functions help break our program into smaller and modular chunks. As our program grows |
| 66 | +# * larger and larger, functions make it more organized and manageable. |
| 67 | +# * It avoids repetition and makes the code reusable. |
| 68 | + |
| 69 | +def greet(): |
| 70 | + print("hello") |
| 71 | + print("how are you") |
| 72 | +greet() |
| 73 | + |
| 74 | +#print greet values 3 times (again again) |
| 75 | +def greet(): |
| 76 | + print("hello") |
| 77 | + print("how are you") |
| 78 | +greet() |
| 79 | +greet() |
| 80 | +greet() |
| 81 | + |
| 82 | +#add function |
| 83 | +def add(a,b): |
| 84 | + c = a+b |
| 85 | + print("The result of c is: ", c) |
| 86 | +add(20,30) #output 50 |
| 87 | + |
| 88 | +# Calculatong factorial in function |
| 89 | +def factorial(n): #"""Return the factorial of a number.""" |
| 90 | + if n == 0: |
| 91 | + return 1 |
| 92 | + else: |
| 93 | + return n * factorial(n - 1) |
| 94 | + |
| 95 | +print(factorial(5)) # Output: 120 |
| 96 | + |
| 97 | + |
| 98 | +# Lambda Functions: In Python, an anonymous function is a function that is defined without a name. |
| 99 | +add=lambda a,b:a+b |
| 100 | +print(add(10,20)) #output 30 |
0 commit comments