0% found this document useful (0 votes)
12 views11 pages

Python Basics: 50 Simple Programs

The document contains a series of Python programming exercises that demonstrate basic programming concepts such as printing messages, performing arithmetic operations, taking user input, and implementing control structures like loops and conditionals. Each exercise includes a brief description and the corresponding code snippet. The exercises cover a wide range of topics, from simple calculations to more complex tasks like checking for prime numbers and manipulating strings.

Uploaded by

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

Python Basics: 50 Simple Programs

The document contains a series of Python programming exercises that demonstrate basic programming concepts such as printing messages, performing arithmetic operations, taking user input, and implementing control structures like loops and conditionals. Each exercise includes a brief description and the corresponding code snippet. The exercises cover a wide range of topics, from simple calculations to more complex tasks like checking for prime numbers and manipulating strings.

Uploaded by

Rohit Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Print Hello World

# Print a simple message

print("Hello World")

2. Print your name

# Print name

print("My name is Karan Kaushal")

3. Add two numbers

# Add two numbers

a = 10

b = 20

sum = a + b

print("Sum =", sum)

4. Subtract two numbers


# Subtract two numbers
a = 20
b = 10
print("Difference =", a - b)
5. Multiply two numbers

# Multiply two numbers

a=5

b=4

print("Product =", a * b)

6. Divide two numbers

# Divide two numbers

a = 20

b=5

print("Division =", a / b)

7. Take input from user


# Take input from user

name = input("Enter your name: ")

print("Hello", name)

8. Check even or odd

# Check if number is even or odd

num = int(input("Enter a number: "))

if num % 2 == 0:

print("Even number")

else:

print("Odd number")

9. Find maximum of two numbers

# Find maximum number

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

if a > b:

print("Maximum is", a)

else:

print("Maximum is", b)

10. Check positive or negative

# Check positive or negative number

num = int(input("Enter a number: "))

if num > 0:

print("Positive")

elif num < 0:

print("Negative")
else:

print("Zero")

11. Print numbers from 1 to 5

# Print numbers from 1 to 5

for i in range(1, 6):

print(i)

12. Print numbers from 5 to 1

# Print numbers from 5 to 1

for i in range(5, 0, -1):

print(i)

13. Sum of first 10 numbers


# Sum of first 10 natural numbers
sum = 0
for i in range(1, 11):
sum += i

print("Sum =", sum)


14. Print multiplication table

# Print multiplication table

num = int(input("Enter number: "))

for i in range(1, 11):

print(num, "x", i, "=", num * i)

15. Find factorial

# Find factorial of a number

num = int(input("Enter number: "))

fact = 1

for i in range(1, num + 1):

fact *= i
print("Factorial =", fact)

16. Check leap year

# Check leap year

year = int(input("Enter year: "))

if year % 4 == 0:

print("Leap Year")

else:

print("Not a Leap Year")

17. Reverse a number

# Reverse a number

num = int(input("Enter number: "))

rev = 0

while num > 0:

digit = num % 10

rev = rev * 10 + digit

num //= 10

print("Reversed number =", rev)

18. Check palindrome number

# Check palindrome number

num = int(input("Enter number: "))

temp = num

rev = 0

while num > 0:

rev = rev * 10 + num % 10

num //= 10

if temp == rev:

print("Palindrome")
else:

print("Not Palindrome")

19. Print first 10 even numbers

# Print first 10 even numbers

for i in range(2, 21, 2):

print(i)

20. Count digits in a number

# Count digits in a number

num = int(input("Enter number: "))

count = 0

while num > 0:

count += 1

num //= 10

print("Number of digits =", count)

21. Simple calculator

# Simple calculator

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

print("Addition =", a + b)

print("Subtraction =", a - b)

print("Multiplication =", a * b)

print("Division =", a / b)

22. Check vowel or consonant

# Check vowel or consonant

ch = input("Enter a character: ")

if ch in "aeiouAEIOU":

print("Vowel")
else:

print("Consonant")

23. Print square of numbers

# Print square of numbers from 1 to 5

for i in range(1, 6):

print(i, "square =", i*i)

24. Print cube of numbers

# Print cube of numbers

for i in range(1, 6):

print(i, "cube =", i**3)

25. Find largest in list

# Find largest number in list

numbers = [10, 5, 20, 8]

print("Largest =", max(numbers))

26. Find smallest in list

# Find smallest number in list

numbers = [10, 5, 20, 8]

print("Smallest =", min(numbers))

27. Sum of list elements

# Sum of list elements

numbers = [1, 2, 3, 4, 5]

print("Sum =", sum(numbers))

28. Length of string

# Find length of string

text = "Python"

print("Length =", len(text))

29. Convert Celsius to Fahrenheit


# Celsius to Fahrenheit

c = float(input("Enter temperature in Celsius: "))

f = (c * 9/5) + 32

print("Fahrenheit =", f)

30. Check number is prime

# Check prime number

num = int(input("Enter number: "))

flag = True

for i in range(2, num):

if num % i == 0:

flag = False

break

if flag and num > 1:

print("Prime")

else:

print("Not Prime")

31. Print star pattern

# Print star pattern

for i in range(1, 6):

print("*" * i)

32. Swap two numbers

# Swap two numbers

a=5

b = 10

a, b = b, a

print("a =", a, "b =", b)

33. Find ASCII value


# Find ASCII value of character

ch = input("Enter a character: ")

print("ASCII value =", ord(ch))

34. Convert string to uppercase

# Convert string to uppercase

text = "python"

print([Link]())

35. Convert string to lowercase

# Convert string to lowercase

text = "PYTHON"

print([Link]())

36. Check number is Armstrong

# Check Armstrong number

num = int(input("Enter number: "))

temp = num

sum = 0

while temp > 0:

digit = temp % 10

sum += digit ** 3

temp //= 10

if sum == num:

print("Armstrong Number")

else:

print("Not Armstrong")

37. Count vowels in string

# Count vowels in a string

text = input("Enter string: ")


count = 0

for ch in text:

if ch in "aeiouAEIOU":

count += 1

print("Vowels =", count)

38. Reverse a string

# Reverse a string

text = input("Enter string: ")

print(text[::-1])

39. Check palindrome string

# Check palindrome string

text = input("Enter string: ")

if text == text[::-1]:

print("Palindrome")

else:

print("Not Palindrome")

40. Print list elements

# Print list elements

numbers = [1, 2, 3, 4]

for num in numbers:

print(num)

41. Create a simple function

# Simple function

def greet():

print("Hello Python")

greet()

42. Function with arguments


# Function with arguments

def add(a, b):

return a + b

print(add(10, 20))

43. Check number divisible by 5

# Check divisible by 5

num = int(input("Enter number: "))

if num % 5 == 0:

print("Divisible by 5")

else:

print("Not divisible by 5")

44. Print odd numbers

# Print odd numbers from 1 to 10

for i in range(1, 11, 2):

print(i)

45. Convert km to miles

# Convert km to miles

km = float(input("Enter kilometers: "))

miles = km * 0.621

print("Miles =", miles)

46. Check string is empty

# Check empty string

text = input("Enter string: ")

if text == "":

print("String is empty")

else:

print("String is not empty")


47. Print dictionary

# Print dictionary elements

student = {"name": "Sandeepa", "age": 22}

for key, value in [Link]():

print(key, ":", value)

48. Count characters in string

# Count characters

text = input("Enter string: ")

print("Characters =", len(text))

49. Generate random number

# Generate random number

import random

print([Link](1, 10))

50. Check number is greater than 100

# Check number greater than 100

num = int(input("Enter number: "))

if num > 100:

print("Greater than 100")

else:

print("Less than or equal to 100")

You might also like