Reading

Interactivity

One of the big differences between programming and a lot of the machines and such that came before it was the difference in how you could interact and customize things.

The key thing about lots of programs that you will write is that they have to interact and change with the user. The easiest way I’ll show here is command line input and output, but fundamentally the others are pretty similar. Complex in details but core ideas stay the same.

How to learn programming the proper way

Throughout, not only will I be teaching you, but I will teach you how to learn.

For now follow this:

  • Every single piece of code that I show you will type by hand. No copy paste, no I can see what it does it’s pretty obvious, no “I already know this, let me move on.” I want you to do this to make sure that you actually process all this. Only copy paste if I tell you that you can. Trust me, this may feel like a small difference but I’ll say this will reduce the effectiveness of whatever you learn to about 10%.

  • Attention to detail: comes from the last point, but when you type everything and look at everything you are forced to concentrate on what you are doing. Pay attention to the difference between 5 and "5" for example. Programming is highly precise and especially in languages like Python every tiny detail matters.

Programming

From last time was hello world. A pretty much programming rite of passage for anyone learning to program for almost 50 years now. Let’s build up from there.

Say hello to anyone

Now the first thing to notice was in

print("Hello world")

is that in the part inside the quotes can be anything.

>>> print("Hello Harsh!")
Hello Harsh!
>>> print("Hello Saloni!")
Hello Saloni!

Changing it it just a matter of changing what’s insite, but let’s go one step further.

Variables

Variables are how computers store and remember things. And each thing we give a name so that we, the programmer, know what it’s doing.

The format (programmers use the word syntax) for it is simple

[name] = [value]

Here are a few examples

>>> x = 5
>>> y = 45.3
>>> name = "Harsh"

And they can be used with print. This is powerful, yet not seemingly so. because the variables here can be anything, it means that so long as the variables are in place your program will work for every case. No one has a use for a caclulator that can only add 1+3, they want it to add any two numbers

>>> print(x)
5
>>> print(name)
Harsh

One thing to note is that these feel similar to math variables. They are not.

>>> x = 5
>>> print(x)
5
>>> x = 6
>>> print(x)
6

This isn’t allowed in math, variables can’t change in there. That’s why it’s always better to think of variables as things your computer can store and remember.

Naming variables

One important priniciple of programming is that you write for multiple audiences.

  • The people who use your program
  • The people who pay for your program (sometimes the same as the first, but not always)
  • The people who read your program (including you)
  • And almost forgot, your computer.

This means you write programs for people, and they happen to also work on a computer.

Which means, write code in a way that’s simple, no bs, elegant and readable. Computers don’t complain, they can deal with everything. Those variable names we gave (x, y, name), the computer doesn’t care what you call them, it can be anything random like electric_boogaloo and the computer handles it all the same. Heck some languages will let you give memory locations on the computer directly (which is something you never do unless you know what you’re doing).

What the computer wants

  • Variables must start with letters or underscores
    • first_name
    • _tax_rate
    • 4chan_url - NOT VALID - starts with number
  • The rest of the name can have letters, numbers and underscores
    • _4chan_url
    • class_of_2022
    • top5_cs_
    • first-name - NOT VALID - the dash means substract for computers
  • Case sensitive. first_name vs First_Name refer to different things, older languages used to not care but in modern languages these do make a differnce. Normally your code should not rely on this but billions of dollars of bugs have come from this so be aware.

What the humans want

The requirement for those are quite broad and pretty much anything qualifies. This is a valid name for example

ur238re80w9fshdfiahw3r23iuo4h2342830r948w90erfudv0sdofnhweiorh2io34huifhiosdfi

But that makes no sense at all to you or someone else reading your code. The good rule of thumb is this, I’ll teach the finer details later.

  • snake_case - this means lower case letters with underscores for spaces
    • tax_rate
    • total_bill
    • hours_worked_per_week
  • Descriptive - They say what they are for
    • compound_interest vs c
    • sum vs nyan_cat
  • Don’t use Upper case I, lower case l or upper case O
    • They look like a lot like 0 and 1 and confuse people a lot. I have lost several nights to these. Trust me.

With autocomplete and IDEs, there is honestly no effort you really save with shorter names. Having better names pretty much always helps. It helps you debug things, it helps you solve things, it makes others happier with your code. And the computer can work with everything.

Adding

The things inside the quotes (“”) that we have been using are strings, that’s the most common ways computers represent any text. This is a type of data you will use a lot wherever you go and it’s all the same at the end of the day.

Just numbers

>>> number1 = 5
>>> number2 = 6
>>> number1 + number2
11

Fair enough, that’s just like math.

Just strings

But when we add strings(text), we get to

>>> string1 = "Hello "
>>> string2 = "world!"
>>> string1 + string2
'Hello world!'

It adds them side by side. This is called concatenation. We use the same symbol as the numbers one but here it means something totally different. Programming and math tend to do this a lot, the same symbol but that do different things, I will explain why this happens and it’s not by accident’

String + numbers for strings

Now let’s take strings with numbers.

>>> string1 + number1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int

Our first error! Now don’t panic. Computers happen to be very awkward and bad at explaining themselves (I fear this often stems from the awkward and bad at explaining themselves nerds who make these languages in the first place).

Usually errors have a lot of fluff so coming to just what information is important is step one.

  • Traceback - that means where things went wrong
  • line 1 - well we only have one line that we entered so it’s within that
  • TypeError: - now you won’t know this yet, but you’ll get used to it (it’s usually 5% basic knowledge and 95% googling to figure out what it means) - I’ll explain it comes from when there is an error regarding types (like strings or numbers here)
  • must be str, not int - you will get used to dealing with the awkward computer soon, but str stands for string and int stands for integers, which is how python handles the numbers we gave it. Here it’s basically saying that the second thing should be a string.

So it wants the integer to become a string. So how do you do that

>>> str(5)
'5'

str() is a command that converts to strings.

So to add our thing

>>> string1 + str(number1)
'Hello 5'

Strings + Numbers for Numbers

>>> birth_year = 2000
>>> age = "19"
>>> birth_year + age
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Wait this looks familiar. The message is different but the issue is the same, the types don’t work. That means we need to convert

>>> int(age)
19

int() is a command that converts to integers - that is the form of numbers we have been using

So,

>>> birth_year + int(age)
2019

Types

We have seen two types of data above, there are many more but I think these two are essesstial for any programming now.

  • Integers - These are for numbers. Like the math integers it’s any whole numbers that can be positive, negative or zero. Ex. -5, 0, 4325234

  • Strings - This is how we represent text as a series of characters. Ex. “Hello”, “12345”, “99 bottles of beer”. In programs they are always surrounded by quotes.

User input

But wait, with what we know, only programmers can set our variables. Doesn’t this mean that anyone who has to use our stuff has to get how our programs work?

Which is why we have user input. So the user can enter things which can be stored and operated from. This is a key aspect of any usable program out there.

The simplest is to take it from the user in the command line, and later I will show more complex versions (user interfaces, websites, apps, apis)

To get user input we use input()

>>> input()
Hi there
'Hi there'

Now we can save that into a variable or do whatever we want with it, it is just a string.

It can even prompt the user to specify what to input like

>>> input("Hi, what's your name: ")
Hi, what's your name: Harsh, Deep

Putting everything together

>>> age = input("Enter your age: ")
Enter your age: 18
>>> age = int(age)

>>> current_year = 2019
>>> possible_year1 = current_year - age
>>> possible_year2 = current_year - age - 1

>>> print("You were either born in " + str(possible_year1) + " or " + str(possible_year2))
You were either born in 2001 or 2000

I can explain this if you prefer, but I want you to see and understand.

Assignments

Programming is something you learn by doing. Which means it never sinks in until you actually write the code. I’ve had lots of times where I think I got something until I opened my editor, and then realized waait… I don’t know anything.

  • Show me some evidence you ran every bit of code above yourself. Screenshots can do for now. Same for the last lesson.

  • We had a hello harsh and hello saloni up there earlier, make a program that takes in someone’s name and says hello to them. It can look something like

Note: only output

Hi, welcome to my program.
What is your name? Harsh Deep
Hello Harsh Deep! You have a very nice name!
  • len() is a command that gives the length of a string. Take the user’s name and print out the length.

Note: only output

What is your name? NyanCat420
10 characters long