Python Programming Examples


The best way to learn about program languages is by following your example.
You are advised to refer to this example and try again.
All of these programs are tested and used for all parts of Python3.

Do not hesitate to use the code of your system.

 Popular examples:

Ø  Add Two Numbers
Ø  Print Hello world!
Ø  Check whether a number is prime or not
Ø  Make a Simple Calculator
Ø  Find the Factorial of a Number

Python Program to Add Two Number

In this program, you will learn to add two numbers to show by using the printed technique
The following, the calculator (+) is used to add two numbers.

Source Code:

# This program adds two numbers
 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

Python program to Print Hello world!

A simple program that shows "Hello, the world!" It is often used to describe the Somali language.

Source Code:

# This program prints Hello, world!
 print('Hello, world!')

Output:

Hello, world!

Python Program to Check Prime Number

In this program, we are working on posting () to print Hello, the world! Display
A string is a series of characters. In Python, the lines are attached to one source, quoted quotes or third quotes ('' '').
Python program and checking basic numbers
Examples to make sure that a mark is a prime number or you do not use the circle and if ... Else. If the number is not important, it will now be published if it is not the first.
A good image than 1 for other issues, except 1 and the number itself is the original number. 2, 3, 5, 7 and others are basic figures since they do not have anything else. 6 but not primarily from 2 x 3 = 6.

 Source Code:

# 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 the 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

Python programs to create a simple calculator

In this example, you will learn how to create a simple calculator that you can add, subtract, multiply or divide the user base.

Source Code:

# 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

Python Program to Find the Factorial of a Number:


In this article, you will learn to find the one-digit 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 points, 0! = 1

Source Code:

# 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 institute in Chennai | Python Course institute in Chennai | Python training in Bangalore | Python training institute in Bangalore Python training in Electronic City Python training in Jaya Nagar | Python training in Pune | Python training institute in Pune | Python training in OMR | Python training in Velachery | Python training in Tambaram | Python training in Annanagar | Python training in Marathahalli Python training in Btm | Python Online Training | Online Certification Course | Python Course in Chennai Python interview questions and answers | Python tutorials | Python training in Indira Nagar | Python Course in Btm | Python Course in Marathahalli Python training institute in Marathahalli Python course institute in Btm Python course in Electronic City | Python online training in Chennai | Python online training in Bangalore | Python Interview Questions & Answers .

Comments

  1. Thank you for sharing .The data that you provided in the blog is informative and effective.python training in bangalore

    ReplyDelete

Post a Comment

Popular posts from this blog

Python – Functions

What is Django in python?

What do you learn about Python?