×

How to write a program in python?

How to write a program in python?

In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this language.

So, let’s start with some examples that will help us to understand the fundamentals of this language.

The following code shows how we can print Hello World in Python.

print("Hello World")

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

In the next program, we will see how we can input functions and ask the user to provide us a value.

Check out the program for the same.

 a=input(“Enter age: ”)   
 print(“The age is {}”.format(a)) 

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

Now we will look at some more examples where the input() function is incorporated with arithmetic operators.

The first program shows how we can calculate simple interest in Python

 p=int(input("Enter the principal: "))
 r=int(input("Enter the rate: "))
 t=int(input("Enter the time: "))
 si=p*r*t/100
 print("The value for simple interest is {}".format(si)) 

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

On executing the given program, we will be asked to enter the values of p,r, and t and then the expected result would be displayed.

In the next program, we will use the pow() and calculate the volume of a cylinder.

The following program shows how we can calculate the volume of a cylinder.

 pi=3.14
 r=int(input("Enter the value of radius: "))
 h=int(input("Enter the value of height: "))
 vol=pi*pow(r,2)*h
 print("Volume of cylinder is {}".format(vol)) 

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

On executing the given program, we will be asked to enter the values of r and h and then the expected result would be displayed.

In the next program, we will see how basic arithmetic operators work in Python.

The following program shows how we can perform addition, subtraction, division, and multiplication.

 num1=float(input("Enter number1: "))
 num2=float(input("Enter number2: "))
 add=num1+num2
 subt=num1-num2
 mult=num1*num2
 div=num1/num2
 print("Addition of two float numbers is {}".format(add))
 print("Subtraction of two float numbers is {}".format(subt))
 print("Multiplication of two float numbers is {}".format(mult))
 print("Division of two float numbers is {}".format(div)) 

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

On executing the given program, we will be asked to enter the values of num1 and num2 and then the expected result would be displayed.

In the next program, we will calculate the area of a circle, we'll pass the parameters r and h and then we will obtain the value of the area in the output.

The following program illustrates the same-

 pi=3.14
 r=int(input("Enter the value of radius: "))
 area=pi*r*r
 print("Area of circle is {}".format(area)) 

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

In the program given below, we'll use decision-making statements and find out the greatest among the three numbers.

The following program illustrates the same-

 a=int(input("Enter the value of 1st number: "))
 b=int(input("Enter the value of 2nd number: "))
 c=int(input("Enter the value of 3rd number: "))
 if(a>b and a>c):
     print("{} is greatest".format(a))
 elif(b>a and b>c):
     print("{} is greatest".format(b))
 else:
     print("{} is greatest".format(c)) 

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

On executing the given program, we will be asked to enter the values of a,b, and c, and then the expected result would be displayed based on the comparison of values the objects hold.

In the next program, we will see what will be the output when strings and numbers are added.

The following program illustrates the same-

 x=input("Enter the first number: ")
 y=input("Enter the second number: ")
 print(x+y)                #result is the addition of two strings
 x=int(input("Enter the first number: "))
 y=int(input("Enter the second number: "))
 print(x+y)                 #result is addition of two numbers 

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

The program which is given below shows how different mathematical operators can be used.

Here we will calculate the distance between two points.

The following program shows the same-

 x1=int(input("Enter the x-coordinate of first point: "))
 y1=int(input("Enter the y-coordinate of first point: "))
 x2=int(input("Enter the x-coordinate of second point: "))
 y2=int(input("Enter the x-coordinate of second point: "))
 dist=((x2-x1)**2+(y2-y1)**2)**0.5
 print("The calculated distance is {}".format(dist)) 

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

On executing the given program, we will be asked to enter the values of x1,y1, and x2,y2, and then the expected result would be displayed after dist is computed.

In the next program we will look at simple examples related to creating functions in Python.

The first example shows how we can add two numbers with the help of functions.

Here we have defined the function and then called it.

Following program illustrates the same-

 def sum(x,y):
     res=x+y
     print("Sum of {} and {} is {}".format(x,y,res))
 x=int(input("Enter the value of x: "))
 y=int(input("Enter the value of y: "))
 sum(x,y)   

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

The last program shows how we can use lambda while defining functions in Python.

Lambda functions help us to reduce the size of the program. Let us see how it works-

The following program shows how to find a cube of a number

 cube=lambda x:x*x*x
 print("Cube is {}".format(cube(9))) 

INPUT-

How to write a program in python

OUTPUT-

How to write a program in python

In this article, we have seen how we can write programs in Python.


Related Topics

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.

Event Key in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

Python List count() method

Python List count() method The list.count() method returns the number of times x appears in the list. Syntax list.count(x) Parameter x: This parameter represents the value to search for and can contain any iterable (list, set, tuple, etc.) Return This method returns the...

1 minute read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Python elif

Python elif The elif statement is used to check multiple conditions and execute the specific block of statements depending upon the true condition among them. Syntax if expression1: statement elif expression2: statement elif expression3: statement else: statement The elif statement can be optional...

3 minutes read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

What is a Script in Python

Have you ever heard that Python is a scripting language? Or when people address a Python program file as a script? Besides programming, script generally means "a written story for...

3 minutes read.

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and...

2 minutes read.

Python Classification

Identification and grouping of items or concepts into specified categories this process is known the classification. Data can be separated and sorted in data management according to predetermined criteria for...

6 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Simple GUI calculator using PyQt5 in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

4 minutes read.

Python Fit Transform

In machine learning, fit (), transform(), fit transform () methods are provided by scikit learn package. This package is used in model fitting and data processing. These methods are implemented...

2 minutes read.

Python Tkinter Tutorial

What is Tkinter? Tkinter is GUI library of Python. When we integrated Tinkter with Python, it provides an easy and quick way to develop GUI applications. It provides the Tk GUI...

14 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.