0% found this document useful (0 votes)
7 views

Untitled 1

The document contains a comprehensive list of Python code snippets categorized into various topics such as Input/Output, Arithmetic, Conditionals, Loops, Number Manipulation, and Patterns. Each category includes multiple examples demonstrating basic programming concepts and operations. The examples cover a wide range of functionalities from simple input/output to more complex algorithms like calculating GCD, factorial, and generating patterns.

Uploaded by

dhruvvasvani624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Untitled 1

The document contains a comprehensive list of Python code snippets categorized into various topics such as Input/Output, Arithmetic, Conditionals, Loops, Number Manipulation, and Patterns. Each category includes multiple examples demonstrating basic programming concepts and operations. The examples cover a wide range of functionalities from simple input/output to more complex algorithms like calculating GCD, factorial, and generating patterns.

Uploaded by

dhruvvasvani624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1

Python Codes
1. Input/Output - Print Name
python
CollapseWrapCopy
name = input("Enter your name: ") print("Hello,", name)

2. Arithmetic - Add Two Numbers


python
CollapseWrapCopy
a = int(input("Enter first number: ")) b = int(input("Enter second
number: ")) sum = a + b print("Sum:", sum)

3. Conditionals - Check Even/Odd


python
CollapseWrapCopy
num = int(input("Enter a number: ")) if num % 2 == 0:
print("Even") else: print("Odd")

4. Loops - Print 1 to N
python
CollapseWrapCopy
n = int(input("Enter a number: ")) for i in range(1, n + 1):
print(i, end=" ")

5. Loops - Sum of First N Numbers


python
CollapseWrapCopy
n = int(input("Enter a number: ")) sum = 0 for i in range(1, n+1):
sum += i print("Sum:", sum)
2

6. Number Manipulation - Factorial


python
CollapseWrapCopy
n = int(input("Enter a number: ")) fact = 1 for i in range(1, n +
1): fact *= i print("Factorial:", fact)

7. Number Manipulation - Reverse a Number


python
CollapseWrapCopy
n = int(input("Enter a number: ")) rev = 0 while n > 0: digit = n
% 10 rev = rev * 10 + digit n //= 10 print("Reverse:", rev)

8. Conditionals - Largest of Three Numbers


python
CollapseWrapCopy
a = int(input("Enter first number: ")) b = int(input("Enter second
number: ")) c = int(input("Enter third number: ")) if a >= b and a
>= c: print("Largest:", a) elif b >= c: print("Largest:", b) else:
print("Largest:", c)

9. Loops - Fibonacci Series


python
CollapseWrapCopy
n = int(input("Enter number of terms: ")) a, b = 0, 1 for i in
range(n): print(a, end=" ") a, b = b, a + b

10. Number Manipulation - Check Prime


python
CollapseWrapCopy
3

n = int(input("Enter a number: ")) is_prime = True for i in


range(2, n): if n % i == 0: is_prime = False break if is_prime and
n > 1: print("Prime") else: print("Not Prime")

11. Medium: Patterns - Right Triangle


python
CollapseWrapCopy
n = int(input("Enter number of rows: ")) for i in range(1, n + 1):
print("*" * i)

12. Functions - GCD of Two Numbers


python
CollapseWrapCopy
def gcd(a, b): while b: a, b = b, a % b return a a =
int(input("Enter first number: ")) b = int(input("Enter second
number: ")) result = gcd(a, b) print("GCD:", result)

13. Strings - Count Vowels


python
CollapseWrapCopy
s = input("Enter a string: ") count = 0 for char in s: if char in
"aeiouAEIOU": count += 1 print("Vowels:", count)

14. Math - Simple Interest


python
CollapseWrapCopy
p = float(input("Enter principal: ")) r = float(input("Enter rate:
")) t = float(input("Enter time: ")) si = (p * r * t) / 100
print("Simple Interest:", si)
4

15. Number Manipulation - Palindrome Check


python
CollapseWrapCopy
n = int(input("Enter a number: ")) temp = n rev = 0 while temp >
0: rev = rev * 10 + temp % 10 temp //= 10 if n == rev:
print("Palindrome") else: print("Not Palindrome")

Input/Output
16.Print "Welcome" 5 times: for i in range(5): print("Welcome")
17.Take age and print: age = int(input("Age: ")); print("Age:", age)
18.Concatenate two strings: s1 = input(); s2 = input(); print(s1 + s2)
19.Print a number with message: n = int(input()); print("Number:", n)
20.Take float and print: f = float(input()); print(f)
21.Print 1 to 10: for i in range(1, 11): print(i)
22.Take two numbers, print product: a = int(input()); b = int(input()); print(a * b)
23.Print a string in reverse: s = input(); print(s[::-1])
24.Take name and age, print: name = input(); age = int(input()); print(name, age)
25.Print a number squared: n = int(input()); print(n * n)

Arithmetic
26.Subtract two numbers: a = int(input()); b = int(input()); print(a - b)
27.Divide two numbers: a = int(input()); b = int(input()); print(a / b)
28.Find remainder: a = int(input()); b = int(input()); print(a % b)
29.Power of a number: base = int(input()); exp = int(input()); print(base ** exp)
30.Average of three numbers: a = int(input()); b = int(input()); c = int(input()); print((a + b +
c) / 3)
31.Cube of a number: n = int(input()); print(n ** 3)
32.Sum of squares: n = int(input()); sum = 0; for i in range(1, n+1): sum += i**2; print(sum)
33.Product of two numbers: a = int(input()); b = int(input()); print(a * b)
34.Find percentage: marks = float(input()); total = float(input()); print((marks / total) * 100)
35.Sum of even numbers: n = int(input()); sum = 0; for i in range(2, n+1, 2): sum += i;
print(sum)
36.Sum of odd numbers: n = int(input()); sum = 0; for i in range(1, n+1, 2): sum += i;
print(sum)
37.Convert km to miles: km = float(input()); print(km * 0.621371)
38.Convert meters to feet: m = float(input()); print(m * 3.28084)
5

39.Area of square: side = float(input()); print(side * side)


40.Perimeter of rectangle: l = float(input()); b = float(input()); print(2 * (l + b))

Conditionals
41.Check positive/negative: n = int(input()); if n > 0: print("Positive"); else: print("Negative")
42.Check greater of two: a = int(input()); b = int(input()); if a > b: print(a); else: print(b)
43.Check divisible by 3: n = int(input()); if n % 3 == 0: print("Yes"); else: print("No")
44.Check leap year: year = int(input()); if (year % 4 == 0 and year % 100 != 0) or (year % 400
== 0): print("Leap")
45.Check vowel: char = input(); if char in "aeiouAEIOU": print("Vowel"); else:
print("Consonant")
46.Check zero: n = int(input()); if n == 0: print("Zero"); else: print("Non-zero")
47.Find smallest of three: a = int(input()); b = int(input()); c = int(input()); if a <= b and a <= c:
print(a); elif b <= c: print(b); else: print(c)
48.Check multiple of 5: n = int(input()); if n % 5 == 0: print("Yes"); else: print("No")
49.Check if number > 50: n = int(input()); if n > 50: print("Greater"); else: print("Smaller")
50.Check uppercase/lowercase: char = input(); if char.isupper(): print("Upper"); else:
print("Lower")
51.Check profit/loss: cp = float(input()); sp = float(input()); if sp > cp: print("Profit"); else:
print("Loss")
52.Check triangle validity: a = int(input()); b = int(input()); c = int(input()); if a + b > c and b +
c > a and a + c > b: print("Valid")
53.Check equilateral triangle: a = int(input()); b = int(input()); c = int(input()); if a == b == c:
print("Equilateral")
54.Check right triangle: a = int(input()); b = int(input()); c = int(input()); if a**2 + b**2 ==
c**2: print("Right")
55.Check voting eligibility: age = int(input()); if age >= 18: print("Eligible"); else: print("Not
Eligible")

Loops
56.Print 1 to 100: for i in range(1, 101): print(i, end=" ")
57.Print even numbers to 50: for i in range(2, 51, 2): print(i, end=" ")
58.Print odd numbers to 50: for i in range(1, 50, 2): print(i, end=" ")
59.Print table of 5: for i in range(1, 11): print(5 * i)
60.Sum of squares to N: n = int(input()); sum = 0; for i in range(1, n+1): sum += i**2;
print(sum)
61.Print 10 to 1: for i in range(10, 0, -1): print(i, end=" ")
62.Count digits: n = int(input()); count = 0; while n > 0: count += 1; n //= 10; print(count)
63.Print multiples of 4: n = int(input()); for i in range(1, n+1): if i % 4 == 0: print(i)
64.Sum of series (1+2+3…): n = int(input()); sum = 0; for i in range(1, n+1): sum += i;
print(sum)
65.Print first 5 Fibonacci: a, b = 0, 1; for i in range(5): print(a); a, b = b, a + b
66.Print square pattern: n = int(input()); for i in range(n): print("*" * n)
67.Print numbers 1 to N in reverse: n = int(input()); for i in range(n, 0, -1): print(i)
6

68.Print A-Z: for i in range(65, 91): print(chr(i), end=" ")


69.Sum of even digits: n = int(input()); sum = 0; while n > 0: d = n % 10; if d % 2 == 0: sum
+= d; n //= 10; print(sum)
70.Print hollow square: n = int(input()); for i in range(n): if i == 0 or i == n-1: print("*" * n);
else: print("*" + " "*(n-2) + "*")

Number Manipulation
71.Sum of digits: n = int(input()); sum = 0; while n > 0: sum += n % 10; n //= 10; print(sum)
72.Product of digits: n = int(input()); prod = 1; while n > 0: prod *= n % 10; n //= 10;
print(prod)
73.Check Armstrong: n = int(input()); temp = n; sum = 0; while temp > 0: sum += (temp %
10)**3; temp //= 10; if sum == n: print("Armstrong")
74.Check perfect number: n = int(input()); sum = 0; for i in range(1, n): if n % i == 0: sum += i;
if sum == n: print("Perfect")
75.Convert decimal to binary: n = int(input()); bin = 0; i = 1; while n > 0: bin += (n % 2) * i;
n //= 2; i *= 10; print(bin)
76.Convert binary to decimal: bin = int(input()); dec = 0; i = 0; while bin > 0: dec += (bin %
10) * (2**i); bin //= 10; i += 1; print(dec)
77.Find factors: n = int(input()); for i in range(1, n+1): if n % i == 0: print(i, end=" ")
78.Check multiple of 10: n = int(input()); if n % 10 == 0: print("Yes")
79.Find middle digit: n = int(input()); count = 0; temp = n; while temp > 0: count += 1;
temp //= 10; n //= 10**((count-1)//2); print(n % 10)
80.Sum of first N odd numbers: n = int(input()); sum = 0; for i in range(1, 2*n, 2): sum += i;
print(sum)
81.Sum of first N even numbers: n = int(input()); sum = 0; for i in range(2, 2*n+1, 2): sum +=
i; print(sum)
82.Check if number is power of 2: n = int(input()); while n > 1: if n % 2 != 0: print("No");
break; n //= 2; else: print("Yes")
83.Find LCM: a = int(input()); b = int(input()); def gcd(x, y): while y: x, y = y, x % y; return x;
print((a * b) // gcd(a, b))
84.Check if number is odd: n = int(input()); if n % 2 != 0: print("Odd")
85.Check if number is even: n = int(input()); if n % 2 == 0: print("Even")
86.Find square root (manual): n = int(input()); for i in range(n+1): if i * i == n: print(i); break
87.Check if number is multiple of 3: n = int(input()); if n % 3 == 0: print("Yes")
88.Find sum of cubes: n = int(input()); sum = 0; for i in range(1, n+1): sum += i**3; print(sum)
89.Check if number is divisible by 7: n = int(input()); if n % 7 == 0: print("Yes")
90.Find HCF (alternative): a = int(input()); b = int(input()); while a != b: if a > b: a -= b; else: b
-= a; print(a)

Patterns
91.Print reverse triangle: n = int(input()); for i in range(n, 0, -1): print("*" * i)
92.Print square of numbers: n = int(input()); for i in range(1, n+1): print(str(i) * n)
93.Print pyramid: n = int(input()); for i in range(1, n+1): print(" "*(n-i) + "*"*(2*i-1))
7

94.Print diamond: n = int(input()); for i in range(1, n+1): print(" "*(n-i) + "*"*(2*i-1)); for i in
range(n-1, 0, -1): print(" "*(n-i) + "*"*(2*i-1))
95.Print hollow triangle: n = int(input()); for i in range(1, n+1): if i == 1 or i == n: print("*" *
i); else: print("*" + " "*(i-2) + "*")
96.Print numbers 1 to N: n = int(input()); for i in range(1, n+1): print(i, end=" ")
97.Print A to Z pattern: for i in range(65, 91): print(chr(i) * (i-64))
98.Print alternating 0s and 1s: n = int(input()); for i in range(n): print(i % 2, end=" ")
99.Print number triangle: n = int(input()); for i in range(1, n+1): print(str(i) * i)
100.Print reverse number triangle: n = int(input()); for i in range(n, 0, -1): print(str(i) * i)
101.Print 1-2-3 pyramid: n = int(input()); for i in range(1, n+1): for j in range(1, i+1): print(j,
end=""); print()
102.Print star diamond: n = int(input()); for i in range(1, n+1, 2): print(" "*(n-i//2) + "*"*i)
103.Print hollow diamond: n = int(input()); for i in range(1, n+1, 2): print(" "*(n-i//2) + "*" + "
"*(i-2) + "*")
104.Print zigzag pattern: n = int(input()); for i in range(n): if i % 2 == 0: print("*"); else: print("
*")
105.Print number square: n = int(input()); for i in range(n): print("1" * n)

Functions
106.Add two numbers: def add(a, b): return a + b; print(add(int(input()), int(input())))
107.Check even: def is_even(n): return n % 2 == 0; n = int(input()); print(is_even(n))
108.Factorial: def fact(n): return 1 if n == 0 else n * fact(n-1); print(fact(int(input())))
109.Reverse number: def rev(n): r = 0; while n > 0: r = r*10 + n%10; n//=10; return r;
print(rev(int(input())))
110.Check prime: def is_prime(n): for i in range(2, n): if n % i == 0: return False; return True;
print(is_prime(int(input())))
111.Power: def power(b, e): return b ** e; print(power(int(input()), int(input())))
112.Swap numbers: def swap(a, b): return b, a; a, b = swap(int(input()), int(input())); print(a, b)
113.Largest of three: def largest(a, b, c): if a >= b and a >= c: return a; elif b >= c: return b;
return c; print(largest(int(input()), int(input()), int(input())))
114.Sum of digits: def sum_digits(n): s = 0; while n > 0: s += n % 10; n //= 10; return s;
print(sum_digits(int(input())))
115.Area of circle: def area(r): return 3.14 * r * r; print(area(float(input())))
116.Perimeter of square: def peri(s): return 4 * s; print(peri(float(input())))
117.Check palindrome: def is_pal(n): return n == rev(n); print(is_pal(int(input())))
118.Sum of squares: def sum_squares(n): s = 0; for i in range(1, n+1): s += i**2; return s;
print(sum_squares(int(input())))
119.Check vowel: def is_vowel(c): return c in "aeiouAEIOU"; print(is_vowel(input()))
120.Convert C to F: def c_to_f(c): return (c * 9/5) + 32; print(c_to_f(float(input())))

Strings
121.Length of string: s = input(); print(len(s))
122.Check palindrome: s = input(); print(s == s[::-1])
123.Convert to uppercase: s = input(); print(s.upper())
8

124.Convert to lowercase: s = input(); print(s.lower())


125.Count consonants: s = input(); count = 0; for c in s: if c not in "aeiouAEIOU": count += 1;
print(count)
126.Reverse string: s = input(); print(s[::-1])
127.Check if string is empty: s = input(); if s == "": print("Empty")
128.Count spaces: s = input(); print(s.count(" "))
129.Find ASCII of char: c = input(); print(ord(c))
130.Concatenate strings: s1 = input(); s2 = input(); print(s1 + s2)
131.Check if char exists: s = input(); c = input(); if c in s: print("Yes")
132.Count specific char: s = input(); c = input(); print(s.count(c))
133.Remove spaces: s = input(); print(s.replace(" ", ""))
134.First and last char: s = input(); print(s[0], s[-1])
135.Check if all digits: s = input(); print(s.isdigit())

Math & Misc


136.Area of rectangle: l = float(input()); b = float(input()); print(l * b)
137.Volume of cube: s = float(input()); print(s ** 3)
138.Convert F to C: f = float(input()); print((f - 32) * 5/9)
139.Compound interest: p = float(input()); r = float(input()); t = float(input()); print(p * (1 +
r/100)**t - p)
140.Distance between points: x1 = float(input()); y1 = float(input()); x2 = float(input()); y2 =
float(input()); print(((x2-x1)**2 + (y2-y1)**2)**0.5)
141.BMI calculator: w = float(input()); h = float(input()); print(w / (h**2))
142.Electricity bill: units = float(input()); if units <= 100: print(units * 5); else: print(100 * 5 +
(units-100) * 10)
143.Check perfect square: n = int(input()); for i in range(n+1): if i*i == n: print("Yes"); break
144.Print all primes to N: n = int(input()); for i in range(2, n+1): if all(i % j != 0 for j in
range(2, i)): print(i)
145.Generate random number: import random; print(random.randint(1, 10))
146.Days in a month: month = int(input()); if month in [4, 6, 9, 11]: print(30); elif month == 2:
print(28); else: print(31)
147.Slope of line: x1 = float(input()); y1 = float(input()); x2 = float(input()); y2 =
float(input()); print((y2-y1)/(x2-x1))
148.Midpoint of line: x1 = float(input()); y1 = float(input()); x2 = float(input()); y2 =
float(input()); print((x1+x2)/2, (y1+y2)/2)
149.Check if number is multiple of 9: n = int(input()); if n % 9 == 0: print("Yes")
150.Dice roll simulation: import random; print(random.randint(1, 6))

You might also like