0% found this document useful (0 votes)
24 views5 pages

New - 3 - Input and Output

Uploaded by

jayrathore130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

New - 3 - Input and Output

Uploaded by

jayrathore130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
SSii| Degezat Python Input and Output In Python, we have many inbuilt functions that help obtain data from the user and then display the data after processing some particular logic in the code. You can refer to the below flowchart to clarify how the system of inputting data and displaying it on some devices works. PYTHON I/O Le |> Em | We use the widely used print() statement to display some data on the screen. We can output the particular data on some device(screen) or even in some file. While writing real-world programs, we have to write statements that explicitly output numbers and strings. SSi| Degezat Let's take an example to display a simple text. print(’Welcome to Ssi Digital") Don’t worry much about double or single quotes. Either of them will work fine in python. Before learning more about output in python, let’s understand the actual syntax of the print() function. This function takes some additional arguments other than objects/values that offer more control over the output format. syntax: print(object(s), sep='', end='\n’, file=file, flush=False) For your better understanding of the syntax here we have defined few keywords of the above statement: object(s) are the values to be printed on the screen. They are converted to strings before getting printed. sep keyword is used to specify how to separate the objects inside the same print statement. By default, we have it as sep='*, a space between two objects. end is used to print a particular thing after all the values are printed. By default, we have end as \n, which provides a new line after each print() statement. file is used to specify where to display the output. By default, it is sys.stdout (which is the screen). flush specifies the Boolean expression if the output is False or True. By default, it is False. In Python, the output from the print() goes into a buffer. Using the flush= True parameter helps in flushing the buffer as soon as we use the print() statement. Python Output Formatting Sometimes a user might want to make the output of a code prettier and more attractive to the audience. For this, we have some other inbuilt functions. Here we will discuss the two widely used methods. ° Ssi | Digital We can use the str.format() method. This format() function will make the output more presentable. The following curly braces { } in the code acts as placeholders. We can also specify the order of these variables by putting numbers in the placeholders. # Here we take two variables and do certain operations with it. x=3 y=12 mul=x*y print(‘The value of xis {} and y is {}'.format(x, y)) # Here we are specifying the order of the variables. print(‘{2} is the multiplication of {0} and {1}'.format(x, y, mul) # We can even use keyword arguments to format strings. print(‘Hey! Welcome to {company}. In this article we will learn about {language}’.format(company='Scaler’, language='Python’)) Output: The value of x is 3 and y is 12 36 is the multiplication of 3 and 12 Hey! Welcome to Scaler. In this article we will learn about Python, Besides this function, we can also use the old % method which we used similarly in C language. add =5+6 print("The new number after addition is %d" % add) Output: The new number after addition is 11. —— SSi| Degezat Python Input Till now, we learned about how to display the python output. The programs were not too interactive as we had hard-coded values of the variables. Sometimes users or developers want to run the programs with their own data in the variables for their own ease. To execute this we will learn to take input from the users, in which the users themselves will define the values of the variables. The input() statement allows us to do such things in Python. The syntax for this function is as follows: input(‘prompt Here prompt is the string we want to display while taking input from the user. This is optional. Let’s look at some examples to have more clarity on this topic. Example 1: # Here we take input from the user and then display it. name = input("Enter your first name: ") print(“Hey! " + name) Output: Enter your first name: Prajit Praji Example 2: # Here we will take an integer as input from the user. age = int(input("Enter your age: ")) new =age +1. name = input("Enter your name: ") SSi| Degezat print("Hey! " + name + " Next year you will be " + str(new)) Output: nter your age: Enter your name: Ayush Hey! Ayush Next year you will be 20 Note: By default, the python input() function takes the user’s input as a string. In example 2, we had to convert the age (from string to integer), which was inputted by the user, using the int() along with the input function. We again need to convert the integer (‘new’ variable) into a string for concatenation during printing the output. Example 3: # Here we will take two numbers as input from the user. num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) mul =num1*num2 print("The multiplication of num4 & num2 is: %.2#" % mul) Output: Enter first number: 2.5 {Enter second number: 2 ‘The multiplication of num1 & num2 is: 5.00 (In the print() statement we have mentioned “%.2f”. This “.2” specifies that the answer that is to be printed should be up to 2 decimal places. Note: Instead of int(), we can also use the float() function, allowing users to enter decimal numbers instead of integers.

You might also like