Python Input:
Python’s in-built function input() is used to take input from the user.
Syntax:
input(‘prompt’)
Where prompt is the string that we need to display on the screen while taking input. It is optional to provide a prompt.
Code:
Python Code
x = input("Enter a Number: ")
print(x)
print(type(x))
Output:
Enter a Number: 10
<class ‘str’>
Explanation: In Code, we can observe that we use the input() function to take input from the user. Here “Enter a Number: “ is prompt. If you see, we gave 10 which is an integer, but the type of input is shown as a string. This is because by default whatever input we provide is treated as a string in python. We need to explicitly convert into our desired data type using type conversion.
Code:
Python Code
x = int( input("Enter a Number : ") )
print(x)
print(type(x))
Output:
Enter a Number : 10
<class ‘int’>
Explanation: Here converted the string into an int.
How to take List elements as input:
Suppose you want to take list elements from the user, There are two ways to do it.
- Take list elements one by one, and append them to the list.
- Using map to and list() function.
Taking elements one by one :
Code:
Python Code
n = int(input("Enter the Number of elements in list : "))
List = list()
print("Enter the List elements")
for i in range(0, n):
List.append(int(input()))
print(List)
Output:
Enter the Number of elements in list : 5
Enter the List elements
1
2
3
4
5
[1, 2, 3, 4, 5]
Using map and list() functions :
First, we would split the input by ‘ ‘, for each space-separated input we apply the int function to convert it into int and group all these inputs in a list. In this previous method, we need to provide
The size of the List priorly, but here we don’t need to specify the size.
Code:
Python Code
print("Enter the list elements ")
List = list(map(int, input().split()))
print("List elements are : ")
print(List)
Output:
Enter the list elements
1 2 3 4 5 6 7
List elements are :
[1, 2, 3, 4, 5, 6, 7]
Note: By default, the split function splits the input based on white space i.e ‘ ‘.
Python Output:
Python’s in-built function print() is used to display the output to the screen.
Syntax:
print(object(s), sep=’ ‘ ,end = ‘\n’, file = file, flush = flush)
parameter | Description |
object(s) | Any object, and as many as you like. Will be converted to a string before printed |
sep | (Optional). Specify how to separate the objects, if there is more than one. Default is ‘ ‘. |
end | (Optional). Specify what to print at the end. Default is ‘\n’. |
file | (Optional). An object with a write method. Default is sys. stdout |
flush | (Optional). A Boolean specifies if the output is flushed (True) or buffered (False). Default is False |
Print with Default separator and end :
Code:
Python Code
print("This article belongs to Takeuforward")
print("tuf", "striver", "India")
Output:
This article belongs to Takeuforward
tuf striver India
In the above code, we can see that the second print statement has multiple objects and they are separated by white space (‘ ‘) which is the default separator. And Two print statements are separated by line (‘\n’) which is the default end.
Print with Custom Separator and end :
Code :
Python Code
print("This article belongs to Takeuforwad", end=" / ")
print("striver", "tuf", "India", sep=" & ")
Output:
This article belongs to Takeuforwad / striver & tuf & India
In the above we can observe that two print statements are separated by ‘ \ ‘ and objects in the second print statement are separated by ‘ & ‘.
Python Import:
Import is used to import the modules into the python interpreter. In Python, Modules are simply files with the “. py” extension containing Python code that can be imported inside another Python Program. In simple terms, we can consider a module to be the same as a code library or a file that contains a set of functions that you want to include in your application.
For example, we can import the math module which has some useful predefined mathematical Functions, by importing the math module we can use them in our code.
Code:
Python Code
import math
print("Factorial of 5 is: ", end="")
print(math.factorial(5))
print("Gcd of 100 and 24 is: ", end="")
print(math.gcd(100, 24))
Output:
Factorial of 5 is: 120
Gcd of 100 and 24 is: 4
By Importing the math Module, we are importing all the attributes, and functions into our program. We can also import some specific attributes and functions only, using the from keyword.
Code:
Python Code
from math import factorial, gcd
print("Factorial of 5 is : ", end="")
print(factorial(5))
print("Gcd of 100 and 24 is : ", end="")
print(gcd(100, 24))
Output:
Factorial of 5 is: 120
Gcd of 100 and 24 is: 4
In the above-specified code, we just imported factorial and gcd functions into our code.
Sometimes module names may be large, we can import these modules with the alternative names too. Let’s import the math module as m.
Code:
Python Code
import math as m
print("Factorial of 5 is : ", end="")
print(m.factorial(5))
print("Gcd of 100 and 24 is : ", end="")
print(m.gcd(100, 24))
Output:
Factorial of 5 is: 120
Gcd of 100 and 24 is: 4
In the above code, we imported math as m, so wherever we use math before, we can use m now.
Special thanks to SaiSri Angajala for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article