×

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

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

How to implement classifiers in Python

What is classification? Classification is a type of supervised machine learning problem with a categorical target (response) variable. Given the known label in the training data, the classifier approximates a mapping...

4 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Python Multiprocessing Processor

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Base Case in Recursive function python

In this article, we will learn about Python's base case in a recursive function. Before learning this, let’s first understand what recursion is in Python and the use of recursion in...

6 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

Python String split() method

The string.split() method in Python splits a string into a list and returns a list of the words in the string. If the parameter maxsplit is given, at most maxsplit splits are done. If maxsplit is...

2 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

Importing Numpy in Pycharm

Numpy : Numpy is one of the important library in python. The abbreviation of numpy is “Numerical python” or “Numeric Python”. This library is mainly used to access arrays. Numpy was created...

5 minutes read.

Python String replace() method

Python String replace() method The string.replace() method in Python returns a copy of the string with all occurrences of substring old replaced by new. Syntax replace(old, new[, count]) Parameter old: This parameter represents a substring you want to...

2 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.

Image to Text in python

Processing out information from an image is a very big task in all the fields of work such as in development and business sector. The process of converting an electronical...

3 minutes read.

Python Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 minutes read.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.