Topics Covered: input(), Type Conversion (int(), float(), str()), String Concatenation.
input() Command
• The input() function in Python is used to read a line of text from keyboard.
• It accepts the line as a string by default.
• When you want to work with numbers entered by the user, such as integers or floats, you need
to convert the string returned by input() to the appropriate numerical type using Python's type
conversion functions. Here's how you can handle integers and floats when using input().
• Example 1: age = int(input("Enter your age: "))
• Example 2: age = float(input("Enter your marks: "))
Syntax:
• input(‘prompt’)
or
• variablename = input(‘prompt’)
Note: prompt is a message to display on the screen before accepting the input. This is optional.
Example:
name = input("Enter your name: ")
print("Hello", name)
In this example, the program will pause and display
"Enter your name:", wait for the user to type something followed by Enter, and then store the input in
the name variable. The next print() function then outputs the greeting with the entered name.
Both print() and input() are essential for basic input/output operations in Python, making them
fundamental for interactive applications.
Examples on print and input commands
Question 1: Greet the User
Program Output
name = input("What is your name? ") What is your name? Emily
print("Hello, " + name + "!") Hello, Emily!
Explanation: This program asks the user to input their name and then greets them by name. The
input() function collects user input after displaying the prompt "What is your name?". The print()
function is then used to concatenate and display the greeting message.
Question 2: Calculate Age in Future Year
Program Output
current_age = input("How old are you? ") How old are you? 12
future_age = int(current_age) + 5 In 5 years, you will be 17 years old.
print("In 5 years, you will be " + str(future_age) + " years old.")
Explanation: The program first asks for the user's current age and then calculates their age in 5 years.
The input() function retrieves the age as a string, which is converted to an integer using int(). The
future age is calculated and then converted back to a string to be printed with the message.
Question 3: Favorite Subject
Program Output
subject = input("What is your favorite school subject? ") What is your favorite school subject?
print(subject + " sounds like a lot of fun!") Math
Math sounds like a lot of fun!
Explanation: The user is asked to enter their favorite school subject, and the program responds with a
positive comment about it. The input() function captures the subject, and the print() function outputs a
custom message including the subject.
Question 4: Adding Numbers
Program Output
print("Enter two numbers and I will add them.") Enter two numbers and I will add them.
number1 = int(input("First number: ")) First number: 7
number2 = int(input("Second number: ")) Second number: 8
sum = number1 + number2 The sum of the numbers is: 15
print("The sum of the numbers is: " + str(sum))
Explanation: This program prompts the user to enter two numbers and calculates their sum. Each
number input is converted from a string to an integer. The sum is then calculated and converted back
to a string for display in the final print() statement.
Variables
• Definition: A variable in Python is like a box where you can store information. It holds data
that can be used later in the program.
• Naming: You can name a variable almost anything, but there are some rules:
• The name must start with a letter or underscore (_).
• The name cannot start with a number.
• It can only contain alphanumeric characters and underscores (A-z, 0-9, and _).
• Variable names are case-sensitive (age, Age, and AGE are different variables).
• Creating a Variable: You create (or declare) a variable by assigning it a value with the =
operator.
• Example: age = 12 creates a variable named age and stores the number 12 in it.
Data Types
Python has several data types that define the operations that can be done on the values and the storage
method for each of them. The most common types for beginners are:
1. Integers (int): These are whole numbers without a decimal point.
• Example: age = 12
2. Floating-point (float): These are numbers with a decimal point.
• Example: height = 5.4
3. Strings (str): These are sequences of characters, typically used to represent words or text.
They are enclosed in quotes.
• Example: name = "Emily"
4. Booleans (bool): This type only has two possible values: True and False. It is often used to
keep track of conditions that can be either true or false. Do not use quotes for True or False.
• Example: is_student = True
How to Use Variables
1. Assignment: You can change the value of a variable by assigning a new value to it.
• Example: age = 14 (now age is 14, not 12)
2. Operations: You can perform operations with variables. The operations available depend on
the data type.
• Example with integers: new_age = age + 2 (if age is 12, then new_age will be 14)
• Example with strings: full_name = first_name + " " + last_name (concatenates two
strings with a space between)
Dynamic Typing or Type Conversion
• Python is dynamically typed, which means you don’t need to declare the type of a variable
when you create it. Python figures out the type based on the value you assign.
• You can change the type of data a variable holds by reassigning it to a different type of data.
• Example: age = "twelve" (now age is a string, not an integer)
Understanding these basic concepts will help Grade 7 students get a solid start in learning how to
program in Python, allowing them to handle data and perform various operations effectively in their
coding projects.
Handling Integer Input
To convert the input string to an integer, you can use the int() function. This is useful when you
expect the user to input whole numbers.
Example:
# Ask the user to enter an age and convert it to an integer
age = input("Enter your age: ")
age = int(age) # Convert the input string to an integer
print("You are", age, "years old.")
Note: If the user inputs something that isn't an integer (like letters or decimal numbers), Python will
raise a ValueError. It's often a good idea to handle this possibility with a try-except block to make
your program more user-friendly.
Handling Floating-Point Input
To handle numbers with decimals, use the float() function to convert the input string to a floating-
point number.
Example:
# Ask the user to enter a height and convert it to a float
height = input("Enter your height in meters: ")
height = float(height) # Convert the input string to a
float
print("Your height is", height, "meters.")
Note: Similar to int(), if the user inputs something that isn't a number, using float() will also raise a
ValueError.
String Concatenation
In Python, string concatenation involves joining two or more strings into one using the + operator. To
concatenate strings with other data types (like integers or floats), you must first convert these types to
strings using the str() function to avoid type errors.
Example of String Concatenation:
name = "Alice"
age = 30
height = 5.4
description = name + " is " + str(age) + " years old and " + str(height) + " feet tall."
print(description)
Output:
Alice is 30 years old and 5.4 feet tall.
Challenge Questions
1. Write a program to:
• Take name and birth year as input
• Convert birth year to integer
• Calculate and print current age (assume current year = 2026)
2. Write a program to:
• Take name and marks
• Convert marks to float
• Print a formatted sentence like:
“Student ___ scored ___ marks”