Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Numbers and Variables: Python Does the Math 33

you’ve listed two variables separated by a space). Third, variable
names in Python are case sensitive; that means that if we use all
lowercase letters in a variable name (like abc), then we can only
use the value stored in the variable if we type the variable name
exactly the same way, with the same capitalization. For example,
to use the value in abc, we have to write abc; we can’t use uppercase
letters like ABC. So My_Name is not the same as my_name, and MYNAME
is a different variable name altogether. In this book, we’ll use all
lowercase letters in our variable names, separating words with
the
symbol.
Let’s try a program using some variables. Type the following
code in a new IDLE window and save it as T h ankYou .py.


ThankYou.py


my_name = "Bryson"
my_age = 43
your_name = input("What is your name? ")
your_age = input("How old are you? ")
print("My name is", my_name, ", and I am", my_age, "years old.")
print("Your name is", your_name, ", and you are", your_age, ".")
print("Thank you for buying my book,", your_name, "!")


When we run this program, we’re telling the computer to
remember that my_name is "Bryson" and that my_age is 43. We then
ask the user (the person running the program) to enter their name
and age, and we tell the computer to remember these as the vari-
ables your_name and your_age. We’re using Python’s input() function
to tell Python that we want the user to enter (or input) something
with the keyboard. Input is what we call information that’s entered
into a program while it’s running—in this case, the user’s name
and age. The part in quotes inside the parentheses, ("What is your
name? "), is called the prompt because it prompts the user, or asks
them a question requiring their input.
In the last three lines, we tell the computer to print out the
value we stored in my_name and the other three variables. We even
use your_name twice, and the computer remembers everything cor-
rectly, including the parts the user typed as input.
This program remembers my name and age, asks the user for
theirs, and prints a nice message to them, as shown in Figure 3-1.

Free download pdf