Essential iPhone & iPad Magazine – August 2019

(ff) #1

STEP 5 What you have done here is assign the letters a and b
two values: 2 and 2. These are now variables, which can
be called upon by Python to output, add, subtract, divide and so on for
as long as their numbers stay the same. Try this:
print(a)
print(b)
STEP 7 You can play around with different kinds of variables
and the Print function. For example, you could assign
variables for someone’s name:
name=”David”
print(name)
STEP 6 The output of the last step displays the current values
of both a and b individually, as you’ve asked them to
be printed separately. If you want to add them up, you can use the
following:
print(a+b)
This code simply takes the values of a and b, adds them together and
outputs the result.
STEP 8 Now let’s add a surname:
surname=”Hayward”
print(surname)
You now have two variables containing a first name and a surname and you
can print them independently.
STEP 10 In Python 3 you can separate the two variables with a
space using a comma:
print(name, surname)
Alternatively, you can add the space ourselves:
print(name+” “+surname)
The use of the comma is much neater, as you can see. Congratulations,
you’ve just taken your first steps into the wide world of Python.
STEP 9 If we were to apply the same routine as before, using
the + symbol, the name wouldn’t appear correctly in
the output in the Shell. Try it:
print(name+surname)
You need a space between the two, defining them as two separate values
and not something you mathematically play around with.
AppleUserMAGAZINE

Free download pdf