Python Programming Examples

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

Theory is important, but practice is more important. Understanding how to ride a bike before actually doing so is important. But practicing to ride (actually riding) is more important to becoming an artist in the field of riding.

Therefore, just like this, the theory of Python is important, but actually writing Python programs is more important. Therefore, I've created a lot of programs with 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 be covered in a single article, 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, PyCharm.

Note: Each and every program is 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 the Python programs given in the next article or page. Since each program is well explained, 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, they've been divided into many articles. But here is a list of some popular ones:

Python Demo Programs

The series of Python programming examples has begun with the following article. 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 about it; continue the series from the next article. You'll learn everything there is to know 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 the above program, there are two lines of code; the first line is a comment. Python treats anything that begins with the # (hash) symbol as a comment. The compiler ignores or doesn't execute anything written after #.

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

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

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

produces the same output as the previous program. The first line in the preceding program is commented with #. That is, I've added # before the first line's code, so that the compiler ignores it and this line of code doesn't get executed.

Now the last two lines are used to output the same thing as the previous program but in a different way. That is, instead of directly printing the string (anything inside a single (') or double ('') quote is treated as a string), I've used a variable named text. The string gets initialized in 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 quotes (""). Because if I include a double quote, then the 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 in the Python programming examples series.

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

print("\nHello", name, "\nWelcome to codescracker.com")

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 the ENTER key. Here is the output you'll see:

python programs with output

Using print() in the preceding program, a message reading "Enter Your Name" is printed on the output.And using input(), I've received the name of the user as input, and whatever name the user enters gets initialized to the name variable.

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

print("\nHello", name, "\nWelcome to codescracker.com")

states that:

  • a newline
  • The string "Hello" is then used.
  • the value of name
  • once more a newline
  • yet another string "Welcome to codescracker.com"

gets printed on the output. That is, if the user enters the name ABC, the output will look like this:

python examples programs

Python Program No.3

This program is created using the 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!")
else:
    print("\nIncorrect Guess!")

Here is its initial output:

python programming examples

Now guess a number and type it, say 13, and press the 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 the above snapshot.

By default, when using the input() function to receive input, the received value is treated as a string-type value. Therefore, using the int() method, I've converted the entered value to its integer equivalent.

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

And if either of the two conditions is false, the program flow executes else's body, as shown in the following sample run of the above program with user input 34:

python all programs

Python Program No.4

This is program number four, and it checks whether the user is a robot or not in the most basic way possible.

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

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

Here is its sample run with user input, "yes":

python examples robots checking

Here's another example with user input, "no".

python examples

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

examples python

The above program is written in such a way that the input is first received via input() and then initialized to chk. Now, using if again, I've compared the value of chk with "yes." That is, if its value is "yes," then the 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 the user enters anything that is not equal to "yes," that means that the condition evaluates to be false, so the counterpart of the 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)
else:
    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 the 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

Until you enter the correct password, the result will not be displayed.Here is the output after entering the correct password, which is codescracker:

python examples with output

Last message before starting the series of Python examples

To learn Python practically, you must complete all of the Python programs in this series, beginning on the following page. Practice these Python programs to feel comfortable in the Python world.

Many Python programs are available, ranging from How to Print Hello World to Shutdown and Restart Computer Using Python Code.

Other Language Programming Examples

Python Online Test


« Python Tutorial Next Program »

Follow/Like Us on Facebook




Subscribe Us on YouTube