Python Programming Examples

This article is created to provide you as many programs as possible in Python, from very basic to an advance level. That is, you'll practice with many varieties of programs created in Python language.

Theory is important, but practice is more important. Like understanding about how to ride a bike before actually riding it, is important. But practice to ride (actually riding) is more important to become an artist in the field of riding.

Therefore, just like this, theory of Python is important, but practicing Python programs is more important. Therefore I've created a lot of programs with different-different varieties, so that you can learn, practice and feel much better than ever before.

About Python Programming Examples

Since I've created more than 1000 Python programs and all programs can't cover in single article, therefore I've divided all these programs into many articles. Each article contains more than one Python program

Note - All programs written here are well-tested and executed using Python's famous IDE named PyCharm.

Note - Each and every program provided with its sample output using snapshots. Snapshots are taken while testing the program. Also, all programs are well-explained.

Important - If you're new to Python, don't worry, just continue practicing Python programs given from next article/page. Since each program is well-explained, therefore you'll get to know everything about the topic one by one. And at the end, through this series of Python programs, you'll get to feel much better than ever before.

List of Popular Python Programs

Since I've created more than 1000 Python programs in this section of Python programming examples, divided into many articles. But here are the list of some popular ones:

Python Demo Programs

Since from next article, the series of Python programming examples starts. But for now, let's see some programs written in Python over here to get more interest in Python.

Important - If you feel uncomfortable while reading the code given below, don't worry on it, continue the series from next article. You'll get to know everything about Python, one by one.

Python Example No.1

This is the simplest Python program, that uses print() to print the value passed as its argument or inside its braces.


print("Welcome to the World of Python!")

If you run the above program, here is the output you'll see:

python example

In above program, there are two lines of code, the first line is a comment. Anything starts with # (hash) symbol treated as a comment in Python. The compiler ignores or doesn't executes anything written after #.

And the second line uses print() to output the thing written inside its braces. For example, the following Python program:

# print("Welcome to the World of Python!")

text = "Welcome to the World of Python!"

produces the same output as of previous program. In above program, the second line gets commented using #. That is, I've added # before second line's code, so that compiler ignores it and this line of code doesn't gets executed.

Now the last two lines is used to output the same thing as of previous program but in different way. That is, instead of directly printing the string (anything inside single (') or double (") quote treated as a string), I've used a variable named text. The string gets initialized to this variable, and I've printed the value of this variable using print().

Note - While putting the variable text inside the braces of print(), I've not used double quote (""). Because if I include double quote, then text variable gets treated as a string, instead of a variable. And the output you will get, should be text, not Welcome to the World of Python!.

Python Example Program No.2

This is the second demo program of Python programming examples series.

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

print("\nHello", name, "\nWelcome to")

Now if you execute or run this program, here is the initial output you'll see:

python programs

Now supply the input, that is, write your name say Robert and press ENTER key. Here is the output you'll see:

python programs with output

In above program, using print(), a message Enter Your Name: gets printed on output. And using input(), I've received the name of user as input and whatever the name entered by user, gets initialized to name variable.

And using the print() again, I've printed the value of name along with some extra thing. The \n is used to insert a newline. Therefore the following statement:

print("\nHello", name, "\nWelcome to")

states that:

  • a newline
  • then the string Hello
  • the value of name
  • again a newline
  • again another string Welcome to

gets printed on output. That is, for example if user enters name as ABC, then output looks like:

python examples programs

Python Program No.3

This program is created using if-else statement of Python. Let's have a look:

print("Guess a Number: ")
num = input()

num = int(num)
if num>10 and num<20:
    print("\nCorrect Guess!")
    print("\nIncorrect Guess!")

Here is its initial output:

python programming examples

Now guess a number and type it say 13 and press ENTER key to see the output as shown in the snapshot given below:

python programs list

Since I've guessed and typed a number that is greater than 10 and less than 20, therefore I've seen the output as shown in above snapshot.

While receiving the input using input() function, the received value treated as a string type value by default. Therefore using int() method, I've converted the entered value to its integer equivalent.

And using if statement, I've applied a condition, that is num>10. The compiler checks whether the value of num is greater than 10 or not. Since its value (13) is greater than 10, therefore this condition evaluates to be true. Now I've applied and keyword, therefore second condition also evaluates to be true, so that to execute the statement present inside its body. Because the second condition also evaluates to be true, therefore program flow goes inside if's body and executes the statement that prints Correct Guess! as output.

And in case, if any of the two condition evaluates to be false, then program flow executes else's body as shown in the sample run of above program with user input 34 given below:

python all programs

Python Program No.4

This is program no.4, checks whether user is a robot or not in simplest way.

print("Are you a Robot ? ")
chk = input()

if chk == "yes":
    print("\nSorry!\nRobots aren't Allowed here!")
    print("\nWelcome to!")

Here is its sample run with user input yes:

python examples robots checking

And here is another sample run with user input no:

python examples

And this is the third sample run with user input 123:

examples python

The above program is created in a way that, first the input gets received using input() and initialized to chk. Now using if again, I've compared the value of chk with "yes". That is, if its value is "yes", then condition evaluates to be true, and program flow goes inside its body and executes the statement that prints a message saying, robots are not allowed!.

But if user enters anything, that is not equal to "yes", means that the condition evaluates to be false, so the counterpart of if statement, that is else's statement(s) gets executed.

Python Program No.5

This is the last demo program of this python exercise (examples) series.

print("Create a Password: ")
cp = input()

print("\nEnter Two Numbers to Add: ")
numOne = int(input())
numTwo = int(input())

print("\nEnter Password to Display the Result: ")
ep = input()

if cp == ep:
    sum = numOne + numTwo
    print("\nResult = ", sum)
    print("\nWrong Password!")

Here is the initial output produced after running the above program:

programming examples python

Now create a password say codescracker and press ENTER key. Here is the output you'll see:

python programs examples with output

Now enter any two numbers say 30 and 60, one by one. Here is the output after supplying these two numbers:

python programs examples list

Now the result will not get displayed until you enter the correct password. Here is the output after entering the correct password, that is codescracker:

python examples with output

Last Message before Starting the Series of Python Examples

To learn Python practically, you must have to go through all python programs in this series coming from next page. Practice these Python programs to feel comfortable in python world.

You will get many Python programs from how to Print Hello World to Shutdown & Restart Computer using python code.

Other Language Programming Examples

Python Online Test

« Python Tutorial Next Program »