LET’S LEARN
PYTHON
Lesson 2:
Variables and input()
Spot the error!
1. print(Python is fun)
2. ‘‘‘ I can write multiline comments
enclosed within quotes like this.
It is used to make my code more
readable”””
3. #This is another example of multiline comment.
#Where I use hash before each line
#Do you think it will work?
❑Identify and use basic Python commands (print
and input).
❑Define and use variables to store different
types of data (strings, integers).
❑Create a simple Python program to print
personalized messages using variables and user
input.
What are Variables?
•A variable stores information that we can
use in our program.
•Think of it as a "container" for data.
•Example: name = "Aisha" stores the name
in the variable name.
• A variable can have a short name (like
x and y) or a more descriptive name
(age, student_name, total_volume).
• A variable name must start with a
letter or the underscore character
• A variable name cannot start with a
Points to number
note about • A variable name can only contain
alpha-numeric characters and
variables! underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive
(age, Age and AGE are three different
variables)
• A variable name cannot be any of
the Python keywords.
Variable Names
Legal variable names: Illegal variable names:
myvar = "John" 2myvar = "John"
my_var = "John" my-var = "John"
_my_var = "John" my var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
•String: Text (e.g., "Hello")
•Integer: Whole numbers (e.g., 13)
Types of •Float: Decimal numbers (e.g., 3.14)
•Boolean: True or False
Data -
•Example Code:
Data name = "Aisha"
Types age = 12
grade = 7.5
reported = True
Printing Variables
To see a variable’s value, use print().
Example Code:
name = "Aisha"
Note that there are no
print(name)
quotes around a variable
If you want to print multiple values or formatted String, use comma.
By default, comma separates values with a space.
Example:
name = "Aisha"
print(“My name is:”, name) # Outputs: My name is: Aisha
Activity – My Info Program
➢Try printing different types of
data!
•Create variables for your Name,
Age, Student ID, Email Address,and
Phone no.
•Use print() to display each one in
a well-formatted way.
Activity – My Info Program
•Code Example
name = "Aisha"
age = 12
student_id=71046
email=“[email protected]”
ph_no=“557892455”
print(“My name is” , name)
print(“I am” , age , “years old.”)
print(“My student ID is” , student_id)
print(“You can write to me on:” ,email)
print(“Or call on” , ph_no)
Using input() for User
Interaction
input() allows us to get input from users.
Example:
name = input("What is your name? ")
print("Hello," , name)
Task: Modify your code using input()
Activity – Info Program using input()
•Code Example
name = input(“Enter your name:”)
age = input(“How old are you?”)
student_id=input(“Enter your student ID:”)
email= input(“Enter your email ID:”)
ph_no= input(“Enter your contact number:”)
print(“Your name is” , name)
print(“You are” , age , “years old.”)
print(“Your student ID is” , student_id)
print(“Your email id is:” ,email)
print(“Contact number:” , ph_no)
Know More - Multi Words Variable Names
Variable names with more than one word can be difficult
to read.
There are several techniques you can use to make them
more readable:
➢Camel Case
Each word, except the first, starts with a capital letter:
example: myVariableName = "John"
➢Pascal Case
Each word starts with a capital letter:
example: MyVariableName = "John"
➢Snake Case
Each word is separated by an underscore character:
example: my_variable_name = "John"
Today, we learned
about variables, and
input().
Recap
We created a program
to print our info in
a formatted way.
Next time, we’ll
learn to use math in
Python programs!