Tuesday 2 April 2019

Top 10 Python Practicals for Beginners

Top 10 Python Practicals for Beginners


1. Write a Python program to print your name.

Start your Python Shell.

You will get command prompt like (>>>). Write following simple command to print your first message using Python Programming.

>>> print("K Patel")

Output:

K Patel

Congratulations!!! Your first python program is done.


2. Write a Python program to print your name and address.

>>> print("K Patel.\nAhmedabad, Gujarat. Pin. 380009\nIndia.")

Output:

K Patel
Ahmedabad, Gujarat. Pin. 380009
India.


3. Write a Python program to read and print your name and address using the input() function.

Source code:

name = input("Enter name: "
print(name)

address = input("Enter address:")
print(address)

mobile = input("Enter mobile number:")
print(mobile)

Output:

Enter name: K Patel 
K Patel

Enter address: Ahmedabad, India 
Ahmedabad, India

Enter mobile number:9898989*** 
9898989***


4. Write a Python program to print 1 to 10.

Solution 1:

for i in range(111):
   print (i)

Output:
1
2
3
4
5
6
7
8
9
10


Solution 2:

for i in range(111):
   print (i, end=" ")

Output:

1 2 3 4 5 6 7 8 9 10

5. Python program to display Good Morning message based in given time.

Source code:


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

if time < 12:
    print ("Good Morning...")

print("Have a nice time...")

Output:

Enter time:5
Good Morning...
Have a nice time...


6. Python program to check whether given number is positive or not.

Source code:


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

if number > 0:
    print ("It's positive number")
else
    print ("It's not positive number")

Output:

Enter a number: 3
It's Positive number


7. Python program to demonstrate nested-if statement. This program will check whether entered number is positive or negative or zero.

Source code:

number = int(input("Enter a number: "))
if number >= 0:
    if number == 0:
        print("It's Zero")
    else:
        print("It's Positive number")
else:
    print("It's Negative number")

Output:

Enter a number: 5
It's Positive number


8. Python program to print sum of 1 to 100 (1+2+3+...+100) using while loop.

Source code:


sum = 0
num = 1

while (num <= 100):
    sum = sum + num
    num = num + 1
print("The sum is", sum)

Output:

The sum is 5050


9. Python program to print square pattern of star.

Source code:


lines = int(input("Enter number of lines:"))

for i in range(lines):    
   for i in range(lines):        
      print('*', end = '  ')    
   print()

Output:

Enter number of lines:3

*  *  *  
*  *  *
*  *  *




10. Python program to print Z to A alphabets.

Source code:


for i in range(90,64,-1):
    print(chr(i), end=' ')

Output:

Z Y X W V U T S R Q P O N M L K J I H G F E D C B A


* * * * *