Python Programming Examples

Python Programming Examples

The best way to learn about program languages is by following our example. You are advised to refer these examples. All of these programs are tested and used for all parts of Python.

Popular examples:

Add Two Numbers

In this program, the user is asked to enter two program numbers showing the user’s PLAYER number. We use the building to do the door. The string returns the string, so use the float ().
We have added two numbers using operator + math. This operator operates, separates (-), multiplexed (*), distribution (/), diffuser (//) or the other (%) of the two numbers. To find out more about entering the Python accountants.

Example Program:

num1 = 1.5

num2 = 6.3

# Add two numbers

sum = float(num1) + float(num2)

# Display the sum

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

Output:

The sum of 1.5 and 6.3 is 7.8

Print Hello world!

Python is a very simple language, and they have simple expressions. This encourages educators to undertake the program without the repatriation plan. directive is a simple print “Python” directive – just printing line (as well as a new line, such as in C).
There are two main types of prayer, Python 2 and 3. Python Python 2 and 3 are slightly different. This training uses Python 3, which is precisely correct and supporting new features.
For example, the difference between Python 2 and 3 against the printing. Python 2, the “print” text does not work, so it is sent out without it. However, Python 3, is a task and must be taken into account.

Enroll Now: Python Training in Chennai

Example Program:

# This program prints Hello, world!

print('Hello, world!')

 

Output:

Hello, world!

Check whether a number is prime or not

The official number is a naturally-occurring number 1 with a lot of other components than 1 and itself. The key figures are the first ones {2, 3, 5, 7, 11, ….}.
The idea of ​​solving the problem was to go to all the numbers from 2 (N / 2) using the loop and each check when N. divided. If we do not know what the numbers are, we will return false. If we find any number between 2 and N / 2 N then distribute a method that N is the prime minister and the people who are truthful.

Enroll Now: Python Training in Bangalore

Example Program:

# Python program to check if the input number is prime or not

num = 407

# take input from the user

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

# prime numbers are greater than 1

if num > 1:

   # check for factors

   for i in range(2,num):

       if (num % i) == 0:

           print(num,"is not a prime number")

           print(i,"times",num//i,"is",num)

           break

   else:

       print(num,"is a prime number")

# if input number is less than

# or equal to 1, it is not prime

else:

   print(num,"is not a prime number")

 

Output:

407 is not a prime number

11 times 37 is 407

Make a Simple Calculator

Make a simple calculator that can not perform basic functions in mathematics as well as addition, subtraction, multiplication or division dependent on user accept.
Focus:

  1. Users choose the required surgery. Options 1, 2, 3 and 4 are valid.
  2. Two numbers are pictured and one … electricity is used to carry out a particular unit.
  3. Use completing (), separation (), multiplication () and divide () to each function.

Example Program:

# Program make a simple calculator that can add, subtract, multiply and divide using functions

# This function adds two numbers

def add(x, y):

   return x + y

# This function subtracts two numbers

def subtract(x, y):

  return x – y

# This function multiplies two numbers

def multiply(x, y):

   return x * y

# This function divides two numbers

def divide(x, y):

   return x / y

print("Select operation.")

print("1.Add")

print("2.Subtract")

print("3.Multiply")

print("4.Divide")

# Take input from the user

choice = input("Enter choice(1/2/3/4):")

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

if choice == '1':

   print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':

  print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':

   print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':

   print(num1,"/",num2,"=", divide(num1,num2))

else:

   print("Invalid input")

 

Output:

Select operation.

1.Add

2.Subtract

3.Multiply

4.Divide

Enter choice(1/2/3/4): 3

Enter first number: 15

Enter second number: 14

15 * 14 = 210

Find the Factorial of a Number

Factory No. 1 of all inventory budgets from 1 to number.
For example, this article 6 (shown in 6) runs on 1 * 2 * 3 * 4 * 5 * 6 = 720. The situation is defined in a number and zero point, 0! = 1

Example Program:

# Python program to find the factorial of a number provided by the user.

# change the value for a different result

num = 7

# uncomment to take input from the user

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

factorial = 1

# check if the number is negative, positive or zero

if num < 0:

   print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

   print("The factorial of 0 is 1")

else:

   for i in range(1,num + 1):

       factorial = factorial*i

   print("The factorial of",num,"is",factorial)

Output:

The factorial of 7 is 5040

To getting expect level training for Python training in your Location – Python Training in Chennai | Python Training in Bangalore | Python Training in Pune | Python Training institute in Chennai Python Interview Questions | Python Course in Chennai

Leave a Reply

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