Basic Programming Examples of Python: Important Examples

Here are some basic programming examples of Python language to give you an idea of how basic programming works in python and which syntax is used to perform any specific task. Examples will be provided in sequential order to get you an easy understanding of the language. So, as usual, first, let’s start with the legendary problem of any programming language- “How to Print Hello World”.

1. How to Print “Hello World” in Python

print("Hello World")
Output:

Hello World

Conditional Statements in Python

2. if demo in Python

x = 20
y = 10
if x > y:
    print("X is greater than Y")
Output:

X is greater than Y

3. if-else(demo 1) in Python

num = input("Enter 0:")
if int(num) == 0:
    print("Zero is Entered.")
else:
    print("You enter another number")
Output:

Enter 0:0
Zero is Entered.

4. if-else(demo 2) in Python

num = input("Enter any number:")
if int(num) <= 50:
    print("Number is less than or equal to 50.")
else:
    print("Number is greater than 50")
Output:

Enter any number:40
Number is less than or equal to 50.

5. if-else with OR operator in Python

gen = input("Are you a Male(y/n):")
if gen == 'y' or gen=='Y':
    print("Gender:Male")
else:
    print("Gender:Female")
Output:

Are you a Male(y/n):y
Gender:Male

6. if-else with AND operator in Python

age = int(input("Enter your age: "))
if age >= 18 and age <= 120:
    print("You are Eligible for Voting")
else:
    print("You are not Eligible for Voting")
Output:

Enter your age: 22
You are Eligible for Voting

7. if-elif(demo 1) in Python

marks = int(input("Enter your Marks : "))
if marks < 35:
    print( "Grade 'F' : Fail")
elif marks >= 35 and marks <= 40:
    print("Grade 'D'")
elif marks >= 41 and marks < 50:
    print("Grade 'C'")
elif marks >= 50 and marks < 60:
    print("Grade 'B'")
elif marks >= 60 and marks < 70:
    print("Grade 'B+'")
elif marks >= 70 and marks < 80:
    print("Grade 'A'")
else:
    print("Grade 'A+")
Output:

Enter your Marks : 69
Grade ‘B+’

8. if elif(demo 2) in Python.

bill_amt = int(input("Enter BilI Amount : "))
if bill_amt >= 5000:
    bill_amt = bill_amt - 500
elif bill_amt >= 4000 and bill_amt <= 4999:
    bill_amt = bil1l_amt - 400
elif bill_amt >= 3000 and bill_amt <= 3999:
    bill_amt = bill_amt - 300
elif bill_amt >= 2000 and bill_amt <= 2999:
    bill_amt = bill_amt - 200
elif bill_amt >= 1000 and bill_amt <= 1999:
    bill_amt = bill_amt - 100
print("Final bill amount after discount is ", bill_amt)
Output:

Enter BilI Amount : 3900
Final bill amount after discount is 3600

9. Nested if in Python.

n = int(input("Enter a number: "))
if n > 10:
    print("Number is greater than to 10")
    if n % 2 == 0:
        print( "Number is Even");
    else:
        print("Number is Odd");
else:
    print( "Number is smaller than or equal to 10");
Output:

Enter a number: 8
Number is smaller than or equal to 10

10. Switch Case alternative ( using dictionary ) in Python

myswitch = {
1: "ONE",
2: "TWO",
3: "THREE",
4: "FOUR",
5: "FIVE"
}
#take user input as integer
num = int(input("Enter any no. (1 to 5): "))
print('You Enter:', myswitch.get(num, "Invalid Number"))
Output:

Enter any no. (1 to 5): 2
You Enter: TWO

Iteration Statements in Python

11. for loop-Accessing Elements in Python

OS = ["Windows", "Mac OS", "Unix", "Linux"]
for x in OS:
    print(x)
Output:

WindowS
Mac OS
Unix
Linux

12. for loop Sequence of chars in string in Python

for x in "Python":
    print(x)
Output:

P
y
t
h
o
n

13. for loop print square of numbers in Python

for x in range(1,11):
    print("Square of" ,x, "is" ,x*x)
Output:

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Square of 6 is 36
Square of 7 is 49
Square of 8 is 64
Square of 9 is 81
Square of 10 is 100

14. for loop print even numbers in Python

print("Even numbers between 1 to 25:")
for x in range(1,26):
    if(x%2 == 0):
        print(x)
Output:

Even numbers between 1 to 25:
2
A
6
8
10
12
14
16
18
20
22
24

15. for loop print multiplicative table in Python

n = int(input("Enter a number: "))
for i in range(1,11):
    print(n*i)
Output:

Enter a number:5
5
10
15
20
25
30
35
40
45
50

16. for-else demo in Python

for x in range(6):
    print(x)
else:
    print("End of loop")
Output:

0
1
2
3
4
5
End of loop

17. range function(demo 1) in Python

for x in range(6):
    print(x)
Output:

0
1
2
3
4
5

18. range function(demo 2) in Python

for x in range(5,10):
    print(x)
Output:

5
6
7
8
9

19. range function(demo 3) in Python

for x in range(0,10,+2):
    print(x)
Output:

0
2
4
6
8

20. range function(demo 4) in Python

for x in range(10,1,-2):
    print(x)
Output:

10
8
6
4
2

21. while loop(demo1) in Python

n = 0
while n != 1:
    print("You are inside the while loop.")
    print("Press 1 to exit from the loop.")
    n = int(input())
print("You are out of the while loop.")
Output:

You are inside the while loop.
Press 1 to exit from the loop:
2
You are inside the while loop.
Press 1 to exit from the loop:
1
You are out of the while loop.

22. while loop(print no.) in Python

n = 1
while n <= 10:
    print(n)
    n += 1
Output:

1
2
3
4
5
6
7
8
9
10

23. while loop(square of number) in Python

n = 1
while n!= 0:
    n = int(input("Enter a number:"))
    print(" Square of the number is ",n*n)
print("End of Program")
Output:

Enter a number:5
Square of the number is 25
Enter a number:3
Square of the number is 9
Enter a number:0
Square of the number is 0
End of Program

24. while-else demo in Python

num= 1
while num <= 5:
    print(num, "is less than 5")
    num += 1
else:
    print("We reached to 5")
Output:

1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is less than 5
We reached to 5

25. nested loop demo in Python

for x in range(1,11):
    for y in range(1,11):
        print("{0:02d}".format(x*y),end=" ")
print();
Output:

01 02 03 04 05 06 07 08 09 10
02 04 06 08 10 12 14 16 18 20
03 06 09 12 15 18 21 24 27 30
04 08 12 16 20 24 28 32 36 40
05 10 15 20 25 30 35 40 45 50
06 12 18 24 30 36 42 48 54 60
07 14 21 28 35 42 49 56 63 70
08 16 24 32 40 48 56 64 72 80
09 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

26. break demo in Python

for x in range(5):
    if(x == 3):
        break
    print("Number is ",x)
Output:

Number is 0
Number is 1
Number is 2

27. continue demo in Python

for x in range(5):
    if(x == 3):
        continue
    print("Number is ",x)
Output:

Number is 0
Number is 1
Number is 2
Number is 4

28. break and continue demo in Python

while (1):
    print("\nEnter a number (<=100) to find its Square.")
    print("Press 0 to exit: ")
    num = int(input())
    if num == 0:
        print("Program's End. Thank You")
        break
    elif num> 100:
        print("Number is greater than 100. Try again.")
        continue
    print("\nSquare of ", num, "is", num * num)
Output:

Enter a number (<=100) to find its Square.
Press 0 to exit:
3
Square of 3 is 9
Enter a number (<=100) to find its Square.
Press 0 to exit
0
Program’s End. Thank You

29. pass(demo 1) in Python

for x in range(5):
pass
Output:

<<no output>>

30. pass(demo 2) in Python

for x in range(5):
    if(x == 3):
        pass
    print("Number is ",x)
Output:

Number is 0
Number is 1
Number is 2
Number is 3
Number is 4

31. pass(demo 3) in Python

#Empty loop
for x in range(5):
    pass
#Empty Function
def func():
    pass
#Empty class
class myClass:
    pass
Output:

<<no output>>

Conclusion

I hope I have given you all the basic programming examples of Python language. One important tip for all python beginners, Please take care of indentation!!. Do you agree with me? Please leave a comment. Hope you like the content and the information shared by me. If you find this post knowledgeable and learned something new and interesting today then please share this post with your friends and family members and help the Optimistic Coder to spread informational contents. Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *