Python Sample: Input-Output, Arithmetic Operations in Python

Python 3 is a modern programming language. It is easy and pleasant to use. There is a print() function to print values in Python. Inside the parentheses we write what we want to print, separated by commas. Here is a program that makes a few calculations:

print (5 + 10)
print (3 * 7 (17 - 2) * 8)
print (2 ** 16) # two asterisks denote exponentiation
print (37/3) # a slash is the division
print (37 // 3) # div operation
print (37% 3) # mod operation

To enter data into the program, we use the input () function. It reads one line.

Here’s a program that reads the name of the user and greets him:

print ( 'What is your name?')
name = input () # read the line and place it into a variable
print ( 'Hello,' + name + '!')

Typically, the program reads the data, processes it and displays a result. When you start it on your computer, the program reads the data that the user entered using the keyboard, and displays the result on the screen.

Let’s try to write a program that reads two numbers and prints their sum. To do this, we consider two numbers and store them in the variables a and b, using the assignment operator =.

a = input()
b = input()
s = a + b

The program will display 57, although in real life 5 + 7 is 12. This is because Python “folded” two lines instead of two numbers. In Python, two lines are formed as follows: the first line is attributed to the second.

In Python, all the data is represented by objects. Number 2 is represented by the object “number 2“, the string ‘hello’ is an object “'string' hello”.

Each object refers to some type. Strings are stored in str type objects, integers are stored in objects of int type, and the fractional numbers (real numbers) are float objects. Object type determines what actions you can do with the objects of this type. For example, if first and second variables are int type objects, they can be multiplied, and if they are objects of type str, they can not be multiplied:

first = 5
second = 7
print(first * second) #will work
first = '5'
second = '7'
print(first * second) #will not work

To convert a string of digits to an integer, use the int () function. For example, int ('23 ') returns the number 23.

Here is an example of the correct program that reads two numbers and prints their sum:

a = int(input())
b = int(input())
s = a + b

This Python sample that you’ve just read was completed by an expert from AssignmentShark. You can use it as an example to complete your own task. It’s absolutely free! We won’t ask you to pay for any of our free samples. Our samples were completed with the aim to help you to understand the topic and to see how theory applies in practice.

When you are working with technical tasks, it is simply not enough to just be knowledgeable in theoretical regularities. You should practice a lot to succeed. If you can’t find enough samples in materials that your teacher provided you with, you can use samples from our website.

This Python sample is not the only one on AssignmentShark. You can find a lot of other Python programming assignments and samples of other disciplines and topics. You can even order a specific example that will be completed especially for you. However, this option is paid. Your task will be executed by an expert that possesses deep knowledge in your topic. Also, it is important to note that we pay attention to our experts’ personal traits. We try to find people that are responsible and diligent. As a result, they will complete a sample for you in a reasonable amount of time.

If you would like to reach the highest level of efficiency with our experts, you should explain all the requirements extremely thoroughly. That way, you won’t need to spend time for further explanations and asking for amendments. You should explain the requirements accurately, especially when you are lacking time. Anyway, don’t hesitate! If you need help, contact us right away to receive assistance!

Leave a Reply

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

Customer testimonials

Submit your instructions to the experts without charge.