|
| 1 | +# Python Programs |
| 2 | +source: `{{ page.path }}` |
| 3 | +## Basic Programs |
| 4 | +#### All Arithmetic operations of 2 no |
| 5 | +```python |
| 6 | +# Store input numbers: |
| 7 | +num1 = input('Enter first number: ') |
| 8 | +num2 = input('Enter second number: ') |
| 9 | + |
| 10 | +# Add two numbers |
| 11 | +add = float(num1) + float(num2) |
| 12 | +# Subtract two numbers |
| 13 | +sub = float(num1) - float(num2) |
| 14 | +# Multiply two numbers |
| 15 | +mul = float(num1) * float(num2) |
| 16 | +#Divide two numbers |
| 17 | +div = float(num1) / float(num2) |
| 18 | + |
| 19 | +# Display the sum |
| 20 | +print(f"The sum of {num1} and {num2} is {add}") |
| 21 | +# Display the subtraction |
| 22 | +print(f"The sub of {num1} and {num2} is {sub}") |
| 23 | +# Display the multiplication |
| 24 | +print(f"The mul of {num1} and {num2} is {mul}") |
| 25 | +# Display the division |
| 26 | +print(f"The div of {num1} and {num2} is {div}") |
| 27 | +``` |
| 28 | +#### python program to swap 2 numbers |
| 29 | +```python |
| 30 | +# method 1 |
| 31 | +x = 1 |
| 32 | +y = 0 |
| 33 | +temp = x |
| 34 | +x = y |
| 35 | +y = temp |
| 36 | +print(f"values of x={x} and y={y}") |
| 37 | +# method 2 |
| 38 | +x = 1 |
| 39 | +y = 0 |
| 40 | +x,y = y,x |
| 41 | +print(f"values of x={x} and y={y}") |
| 42 | +# method 3 |
| 43 | +x = 10 |
| 44 | +y = 50 |
| 45 | + |
| 46 | +# Swapping of two variables |
| 47 | +# using arithmetic operations |
| 48 | +x = x + y |
| 49 | +y = x - y |
| 50 | +x = x - y |
| 51 | +print(f"values of x={x} and y={y}") |
| 52 | +``` |
| 53 | +#### python program convert km to miles |
| 54 | + |
| 55 | +```python |
| 56 | +# converting Kilometer in to miles |
| 57 | +# 1 kilometer is equal to 0.62137 miles |
| 58 | +x = float(input("enter the no of kilometers : ")) |
| 59 | +miles = x * 0.62137 |
| 60 | +print(f" Total no of {x} kilometers in to miles is {miles}") |
| 61 | +``` |
| 62 | + |
| 63 | +#### python program convert celsius to Fahrenheit |
| 64 | + |
| 65 | +```python |
| 66 | + |
| 67 | +## T(℉) = T(℃) x 1.8 + 32 |
| 68 | +C = float(input("enter the celsius value : ")) |
| 69 | +F = ( C * 1.8 ) + 32 |
| 70 | +print(f"The celsius of {C} in Fahrenheit temp is {F}") |
| 71 | +``` |
| 72 | + |
| 73 | +#### Python program to display calender |
| 74 | + |
| 75 | +```python |
| 76 | +import calendar |
| 77 | +# Enter the month and year |
| 78 | +yy = int(input("Enter year: ")) |
| 79 | +mm = int(input("Enter month: ")) |
| 80 | + |
| 81 | +# display the calendar |
| 82 | +print(calendar.month(yy,mm)) |
| 83 | +``` |
| 84 | + |
| 85 | +#### python program of multiplication table |
| 86 | + |
| 87 | +```python |
| 88 | +mul = int(input("Enter the no for multiplication : ")) |
| 89 | +for i in range(1,11): |
| 90 | + print(mul, "*" ,i, "=" ,mul * i) |
| 91 | +``` |
| 92 | + |
| 93 | +#### Pythom program for Leap Year |
| 94 | + |
| 95 | +```python |
| 96 | +year = int(input("Enter a year: ")) |
| 97 | +if (year % 4) == 0: |
| 98 | + if (year % 100) == 0: |
| 99 | + if (year % 400) == 0: |
| 100 | + print(f"{year} is a leap year") |
| 101 | + else: |
| 102 | + print(f"{year} is not a leap year") |
| 103 | + else: |
| 104 | + print(f"{year} is a leap year") |
| 105 | +else: |
| 106 | + print(f"{year} is not a leap year") |
| 107 | +``` |
0 commit comments