Brand New to Python

Hey all,

I’m new to the python programming language and, (with the exception of a single high school class years ago), am new to programming entirely.

I’m now taking an intro to programming fundamentals in a Uni program and have a question about a specific code example that was given to us in a textbook:

##
# Writes down the first initial of you and your significant other
#

# Collects first names
yourFirstName = input("What is your first name? ")
partnerFirstName = input("What is your partner's first name? ")

initials = yourFirstName[0] + " & " + partnerFirstName[0] # Why this step?

print(initials) # Why not just print(yourFirstName[0] + " & " + partnerFirstName[0]?

I put the comments in on the last two lines of the code. Basically, I’m wondering why you’d add the variable “initials” when instead you could ignore the step of adding one more variable and just print firstName[0] and partnerFirstName[0].

Thanks folks and expect to see lots from me as I learn this language lol.

Hi Ryan. You asked:

I put the comments in on the last two lines of the code. Basically,
I’m wondering why you’d add the variable “initials” when instead you
could ignore the step of adding one more variable and just print
firstName[0] and partnerFirstName[0].

No particular reason. You’re have to ask the person who wrote the code.

I can think of a few reasons why somebody might decide to use a
variable.

# Why this?
initials = yourFirstName[0] + " & " + partnerFirstName[0]
print(initials) 

# Rather than just this?
print(yourFirstName[0] + " & " + partnerFirstName[0])

It is a teaching course, and they want to teach you to use variables.

They are going to re-use the initials variable later, so it is less
error-prone, easier to maintain and debug, and probably faster, to
calculate the initials once and store it in a variable than to calculate
it over and over again.

If a line of code is too long or complicated, and so hard to read, you
can simplify it by extracting part of that line out into a variable on a
separate line.

It is often easier to reason about things by naming parts of the thing
rather that referring to the whole each time. For example, which is
easier to understand?

“My two year old nephew pulled the dog’s tail.”

“My two year old nephew pulled the part of the dog that extends out
past the rear of the dog.”

We give that part of the dog a name, “tail”, and now we can refer to it
much more easily.

Using a named variable may express the intention of the programmer
more easily than the calculation itself. print(initials) tells us
exactly what the intention is: to print the initials. Whereas the line
of code

print(yourFirstName[0] + " & " + partnerFirstName[0])

isn’t so clear. Obviously it’s printing something, but what? We start
with your first name, then you extract the first character from that
name (but why?) then concatenate it with the string " & ", then
concatenate the result with the partner’s first name, no wait, not the
whole name, just the first letter… oh, it’s printing your and your
partners initials!

Using named variables often can help make code easier to understand.

Hey - thanks so much for a detailed response. The tail analogy really made a lot of sense. I guess I need to remember that I might not be the only person reading my code.

Hi Ryan,

Two weeks ago, you commented:

“I guess I need to remember that I might not be the only person reading
my code.”

It is not just other people. It is also you, in the future. When you
come back to some of your old code that you haven’t touched for six
months, or six years, and find yourself cursing the idiot who wrote it
because you can’t work out what it is doing, you will soon learn to
write code to make Future You happy as well :slight_smile: