0% found this document useful (0 votes)
6 views15 pages

Python Simple Program

The document contains a series of Python programs that demonstrate basic programming concepts such as checking even/odd numbers, calculating squares and cubes, identifying vowels, and handling dictionaries. It also includes examples of list slicing, triangle classification, sorting student marks, and using the time module. Additionally, it lists intermediate-level programming tasks like generating Fibonacci series, checking prime numbers, and finding common elements in lists.

Uploaded by

harshleen kour
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)
6 views15 pages

Python Simple Program

The document contains a series of Python programs that demonstrate basic programming concepts such as checking even/odd numbers, calculating squares and cubes, identifying vowels, and handling dictionaries. It also includes examples of list slicing, triangle classification, sorting student marks, and using the time module. Additionally, it lists intermediate-level programming tasks like generating Fibonacci series, checking prime numbers, and finding common elements in lists.

Uploaded by

harshleen kour
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.

Python program to check given


number is even or odd
#taking input from the user
num = int(input("Enter a number to check even/odd:" ))
#if number is divisible by 2
if num % 2 == 0:
print(num, "is even number")
else:
print(num, "is odd number")

Output:

Enter a number to check even/odd:9

9 is odd number
2. Python basic programming
questions to calculate the square of a
number
3. num= int(input("finding the square of the number: "))
print("square=",num * num)
output:

finding the square of the number: 4

square= 16

3. Python program to calculate the cube of


a number
num= int(input("calculating the cube of the number: "))
print("square=",num * num*num )

output :
calculating the cube of the number: 2

square= 8

[Link] program to check given character


is a vowel or consonant
Ch = input("Enter a character to check vowel or consonant: ")

if Ch == 'a' or Ch == 'e' or Ch == 'i' or Ch == 'o' or Ch == 'u' or Ch ==


'A' or Ch == 'E' or Ch == 'I' or Ch == 'O' or Ch == 'U':
print("Given character", Ch, "is a vowel")
else:
print("Given character", Ch, "is a consonant")

Output: Enter a character to check vowel or consonant: A


Given character A is a vowel

[Link] add two dictionaries


h={"Name":"harsh" , "Age":12}
b={"hobbies":"readimg books"}

[Link](b)
print(h)

output:

{'Name': 'harsh', 'Age': 12, 'hobbies': 'readimg books'}


6. simple Python program that
demonstrates slicing on a list
Define a list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Print the original list


print("Original List:", numbers)

# Perform slicing to get a sublist


sublist = numbers[2:7] # This gets elements from index 2 (inclusive) to
index 7 (exclusive)
print("Sliced List:", sublist)
Output:

Original List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Sliced List: [3, 4, 5, 6, 7]

[Link] the greater number.


a=int(input("insert the number a =" ))
b=int(input("insert the number b =" ))
if a>b:
print("a is greater")
elif a<b:
print("b is greater")
else:
print('both number are equal')
output: insert the number a =5

insert the number b =4

a is greater

8. write a program that accepts the 3 sides


of a triangle as input:
Isosceles triangle-triangle has 2 equal sides
equilateral triangle-all 3 sides equal
scalene triangle-no equal side
a = int(input("enter the triangle side 1:"))
b = int(input("enter the triangle side 2:"))
c = int(input("enter the triangle side 3:"))

if a == b== c :
print("triangle is equilateral")
elif a == b or b == c or c == a:
print("isosceles")
else:
print("scalene")

output:

enter the triangle side 1:3

enter the triangle side 2:4

enter the triangle side 3:5


scalene

9. write a program to detect double spaces


in a string
10. rite a program to accept marks of 6
students and display them in a sorted
manner.
stu_1=int(input("enter the marks :"))
stu_2=int(input("enter the marks :"))
stu_3=int(input("enter the marks :"))
stu_4=int(input("enter the marks :"))
stu_5=int(input("enter the marks :"))

a=print("marks =",stu_1 , stu_2,stu_3,stu_4,stu_5)

b=[stu_1,stu_2,stu_3,stu_4,stu_5]
[Link]()
print("sorted list",b)
output:

enter the marks :6

enter the marks :2

enter the marks :7

enter the marks :3

enter the marks :5

marks = 6 2 7 3 5

sorted list [2, 3, 5, 6, 7]

[Link] time module to print good


morning ,good evening and good night
import time

timestamp=[Link]("%H:%M:%S")
print(timestamp)
hour=int([Link]("%H"))
print("hour=",hour)
min=int([Link]("%M"))
print("min=",min)
sec=int([Link]("%S"))
print("sec=",sec)

if hour<=12 :
print("good mrng")
elif hour>12:
print("good afternoon")
elif hour>4:
print("good evening")
output:

11:02:01

hour= 11

min= 2
sec= 1

good mrng

12. 5 table using for loop


mul=5
for i in range(1,11):
result=mul*i
print("5*",i,"=",result )
output:

5* 1 = 5
5* 2 = 10
5* 3 = 15
5* 4 = 20
5* 5 = 25
5* 6 = 30
5* 7 = 35
5* 8 = 40
5* 9 = 45
5* 10 = 50

13. using while loop


i=1
while (i <=10):
result=i*5
print("5*",i,"=",result )
i=i+1
Intermediate-Level Python Programs

1. Python program to check given number is even


or odd.
2. Python basic programming questions to calculate
the square of a number
3. Python program to calculate the cube of a
number
4. Python program to check given character is a
vowel or consonant
5. To add two dictionaries
6. simple Python program that demonstrates slicing
on a list
7. Find the greater number
8. write a program that accepts the 3 sides of a
triangle as input:
a. Isosceles triangle-triangle has 2 equal sides
b. equilateral triangle-all 3 sides equal
c. scalene triangle-no equal side
9. write a program to detect double spaces in a string.
10. write a program to accept marks of 6 students
and display them in a sorted manner.
11. use time module to print good morning, good
evening and good night
12. 5 table using for loop
13. 5 table using while loop
14. Fibonacci Series
15. Check Prime Number
16. Reverse a String
17. Count Vowels in a String
18. Merge Two Sorted Lists
19. Find Second Largest Number
20. Check if a Year is Leap Year
21. Find Common Elements in Three Lists
22. Factorial of a Number

You might also like