Recents in Beach

Python Input Output (I/O) and Import

Previous                                                          Next⏩


Python print() function:

In python, if you want to show something on screen as an output, there we use the print() function. Using the print() function you can show the output data on your standard screen.
Normally print() function is used to show the output data of variables and strings.
Let’s understand it with an example:

 
 2
 3
 4
 5
 6
 7
 8
 9
10
x=10
# to print a variable just put it inside the print function
#inside the print function if you do use the quotes it treats as a variable
print("x")                             #here x is a string
print(x)                 #here x is a variable containing a value 10
#use comma, to separate two different data types while using the print option
integer = 10
string = "Hello world"
print(string, integer)

Output :
x
10
Hello world 10

If you write two print statements you would get their two outputs in two lines because the print() function has an additional keyword end inside it which has default value ‘\n’ which is used to print a new line. if you want you can alter the value of end.

Example :

1
2
3
4
print("Hello" , end="#" )
print("world")
print("hello", end="\t")
print("world")

Output :
Hello#world
hello world

String Formatting:
There is a special function called format() which is used with a string, that gives you more flexibility over the print() function. format() function is used with a string and you can insert variables in specific places of that particular string.
We use {} curly brackets to hold the places of variables or string that pass in the format() function.
let’s understand it with an example:
1
2
3
s= "Programokey"
age=20
print("hi, there my name is {} and I am {} years old ". format(s,age))

Output :
  hi,there my name is Programokey and i am 20 years old.

the {} is also known as placeholders for the format() function and the placeholders should correspond to the variables passed in the format() function.
you can also use indexing in the placeholder to call the corresponding values in format() function.
e.g.
1
2
print("i love {1} and {0}".format("mangoes","apples"))
print("i love {0} and {0}".format("mangoes","apples"))

Output :
 i love apples and mangoes
 i love mangoes and mangoes

Input in python using input() function
in the previous examples, we were assigning the values to the variable before we run the programme but what if we want value from the user rather than pre-defining it. Here we use the input() function which allows the user to input the values in the programme.
Here are something you must know before you use an input() function. Whenever you input a value in the programme with the help of input() function the value is treated as a string and what if you want to enter an integer or float for that you need to use typecasting .

#input function syntax

variable = input([prompt])
here whatever value we enter it will store in the variable.
The prompt is a string which is used as a message that displays on the screen
Let’s understand it with an Example
1
2
3
4
5
6
input_1=input("Whatever you enter here treated as a string: ")
integer = int(input("Enter an integer: "))
float_1= float(input("Enter a float: "))
print(input_1)
print(integer)
print(float_1)

Output :
 whatever you enter here treated as a string: 07
 Enter an integer:5
 Enter an float : 34
 07
 5
 34.0

Import in python:
Import is a keyword which is used to import the definitions of the module (modules are the python files which contain the prewritten code or function) in the current file. Import keyword is used along with another keyword from which is used to import the module.
Suppose you want a programme which accepts an integer and give the square root of the integer. For this, you can make a program or you can use a predefined function sqrt() which is a part of module math. So to use the sqrt() function you need to import it on your current file by using the import keyword.
Let’s understand it with an example:
1
2
3
4
from math import sqrt
var = int(input("Enter a Number: "))
sqt = sqrt(var)
print(sqt)

Output :
Enter a Number : 10
3.1622776601683795


Previous                                                          Next⏩



Post a Comment

0 Comments