Python examples showing how to input data
# taking 1 input
x = input("Enter a number: ")
print("The number is", x)
#second method to print since x is of type String
print("The number is " + x)
# taking 2 inputs separately
nb1 = input("Enter first number: ")
nb2 = input("Enter second number: ")
print(nb1, nb2) # 5 6
print(nb1 + nb2) # 56
print(int(nb1), int(nb2)) # 5 6
print(int(nb1) + int(nb2)) # 11
print(int(nb1 + nb2)) # 56
# taking 2 inputs at a time
x, y = input("Enter two values: ").split()
print("Number of x: ", x)
print("Number of y: ", y)
print("x+y", int(x)+int(y))
# taking 3 inputs at a time
x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)
# taking 2 inputs at a time
a, b = input("Enter two values: ").split()
print("First number is {} and second number is {}".format(a, b))
#or
msg = "First number is {} and second number is {}"
print([Link](a, b))
Exercise 1: Write a python program that reads from input the name and the year of birth of a
person and then print a greeting message with the name and age of that person
Sample to Run:
Enter your name and your birth year: Sami 2000
Welcome Sami your age is 23
Solution:
name, yearBirth = input("Enter your name and your birth year").split()
age = 2023 – int(yearBirth)
print("Welcome", name, "your age is", age)
#second print method
print("Welcome " + name + " your age is " + age)
#third print method
print("Welcome {} your age is {}".format(name, age))