0% found this document useful (0 votes)
6 views2 pages

Python Input Data Examples

The document provides various Python examples for taking user input, including single and multiple inputs, and demonstrates different methods for printing the results. It also includes an exercise to create a greeting message based on the user's name and year of birth, along with a sample solution. The examples illustrate the conversion of input strings to integers and formatting output in different ways.

Uploaded by

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

Python Input Data Examples

The document provides various Python examples for taking user input, including single and multiple inputs, and demonstrates different methods for printing the results. It also includes an exercise to create a greeting message based on the user's name and year of birth, along with a sample solution. The examples illustrate the conversion of input strings to integers and formatting output in different ways.

Uploaded by

Full name
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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))

You might also like