codescracker


python

Python Programming Examples



« Python Tutorial Next Program »


Python Examples

To learn python practically, you must have to go through all the given python programs here. Practice these python programs to feel comfortable in python world.

Here you will see each and every python program from how to Print Hello World to Shutdown & Restart Computer using your python code. All python programs provided here along with their respective output.

Let's take a look at some python programs. Before going to all these python programs, first you have to setup your python environment to run all the python programs. Therefore, you can go for Python Environment Setup to learn how to setup python environment.

Note - Here we have also type and run all the programs directly on Python shell. But, we recommend you to "save then run" each and every python program.

Python Program Example No.1

Here is the first python programming example:

# Python Program - Example No.1 
		
print("Enter 'x' for exit.");
val = input();
if val == 'x':
    exit();
else:
    print("Welcome to Python from codescracker.com!");

Here is the sample run of the first python programming example. This is the initial output.

python example

Now let's type something, say codescracker and press ENTER key to see the output as in given screenshot.

python programs

Now let's re-run the above example, and this time, type x as shown in below screenshot:

python programs with output

and then press ENTER to see the window asking to exit the program as shown in the figure given below:

python examples programs

From the above window, confirming to exit or killing the program, click on OK button to kill or exit, and Cancel button to come back to the Python shell window.

Here is the same program directly written and run on Python shell window. In this program, we have used break keyword to break out of the conditional statement in place of exit().

python programming examples

Program Video with Compile and Run

Python Program Example No.2

Here is the second python programming example:

# Python Program - Example No.2
		
print("Enter 'x' for exit.");
val = input();
if val == 'x':
    exit();
else:
    print("Hello,",val,"Sir, You are at codescracker.com\n");

Here is the sample run of the second python programming example. This is first and initial output.

python programs list

Now type any name say abhishek and press enter to see the sample output given here.

python all programs

Here is the same program with run on Python shell.

python examples

Program Video with Compile and Run

Python Program Example No.3

Here is the third python programming example. This is the number guessing program.

In this program, if user guess a number that will be in between 10 to 100, then What a guess..!! will printed and if he/she guessed and typed any other number that doesn't come in that range, then Opps..!! will be printed. Enter 0 to exit from or quit the program.

# Python Program - Example No.3
		
print("Enter '0' for exit.");
val = int(input("Guess a Number: "));
if val == 0:
    exit();
elif(val>10 and val<100):
    print("What a guess..!!\n");
else:
    print("Opps..!!\n");

Here is the sample run of the third python programming example:

This is the initial output.

examples python

Now let's guess and type the guessed number say 12 and then press enter to see the following output.

programming examples python

Here is the same program with run on Python shell.

python programs examples with output

Program Video with Compile and Run

Python Program Example No.4

Here is the fourth python programming example. This is a robot checking program.

In this program, user ask to tell whether he/she is a robot or not. If he/she will enter yes as input then simply he/she can't proceed as robots are not allowed here and in case he/she will enter no as input then he/she can proceed as humans are allowed.

# Python Program - Example No.4
		
print("Enter 'x' for exit.");
check = input("Are you a Robot ? ");
if check == 'x':
    exit();
elif(check == 'yes'):
    print("Sorry!, you can not proceed.\n");
else:
    print("Congrats!, you can proceed.\n");

Here is the sample run of the fourth python programming example:

Below is the initial output:

python examples robots checking

Now type yes or no, say no and press enter to proceed.

python programs examples list

Here is the same program with run directly on python shell.

python examples with output

Program Video with Compile and Run

Programming Examples in Other Languages

You may also like to learn or practice programming examples in other popular programming languages:


« Python Tutorial Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test