Class 2

  1. Reading
  2. Basics of programming
  3. Homework

Reading Class 2

To be familar with the upcoming section, skim through this on basic syntax.

Read through this on variable types.

Skim through this on basic operations.

Read through this on decision making carefully.

When we say carefully, we really mean, try to really understand, the better you understand what is going on, the better you will code, and the less you will debug later.

Optional: Math and numbers Strings Boolean Logic

Basics of programming

Python

Python is a beginner friendly language that has an english-like sytanx. In Python, the interpreter (the thing that will help the computer understand what you are typing) uses spaces or tabs to seperate commands and understand the flow of the program. In other many other languages semicolons and braces ( { } ) are used, or some mixture. You can use semicolons if you'd like, but generally it is not done. Try and stay close to general python coding practices so that others can understand your code better and you will be able to read theirs as well. As you program in Python you will get a better understanding of this.

To understand some of the work that is done here, I write "#" after code sometimes, there are comments. They are english and ignored by the computer. Read them as an english commenter for the line of code or the code your about to read or just read.

variables

In python you can create a variable easily as long as you don't name it one of the keywords. You can read about the keywords here.

If you wanted to put a mathematical expression or a sentence or a mixed list you would do it like this:

x = 5 + 12 + 24 + 36
    sentence = "I love coding in python"
    mixedList = [5 + 23, 0, "Coding is fun", 10.5, "Hello world!"]

To play around with this, open the interpreter (by typing python in your terminal/command line or using an online one here) and try it out.

input and output

If we want to output, we use 'print', it does not matter about the internals here at this point, for our sake lets use it as a function like so:

print("Hello World")

You may want to skim through this before you continue.

Input in python is simple but is different depending on your version. If you are using python 2.7 or less than you want to use raw_input() if your using python 3 or above, you want to use input(). In the class folder there is a helpFunctions.py file, you can read through it if you'd like. To get the users name:

from helperFunctions import *
    name = getString("Please enter a name")

and to print it

print(name)

or

print("Hello " + name)
If statements

If statements are a logical way to make decisions based on unknown input. We use if statements in our lives all the time. For example, we say "if I am late, leave without me", "If I don't catch the bus, I'll take a cab".

In programming we use if statements as a form of controlling the flow of the program. The sytanx is pretty straightforward, you can read about it here, only the first section is necessary.

The syntax, taken from the docs:

if x < 0:
        print('x is negative')
    elif x == 0: #else if
        print('x is zero')
    elif x == 1: #else if
        print('x is one')
    else:
        print('x is positive and not one') # not less than zero, not zero and not one

the if statement checks if x is less than zero, if it is it will print: 'x is negative', if it is not and if (else if) x is zero then print: 'x is zero', if it is not and if x is 1, print: 'x is one', else in all other cases, print: 'x is positive and not one'

This next example is going to be a bit confusing, I do this, not to confuse you on purpose, but to try and drive home a point.

The inside of an if statement need not be a single command, it can be an if statement itself, in fact it can be any number statements, for example:

x = 25
    if x > 10:
        if x > 30:
            if x > 50:
                print("x is really big")
            else: # 30 < x < 50
                print("x is prety big")
        else: # 10 < x < 30
            if x > 20: # 20 < x < 30
                print("x is big")
            else: 10 < x < 20
                print("x is nicely sized")
    else: # x < 10
        print("x is small")

I agree there are better ways to write this, but this is just an example of how you can nest code for more complicated control flow.

The last thing we will do with if statements, is boolean logic. In python if you want to test for multiple conditions in an if statement, you can use keywords like and or or. They are very useful and can make reading code really easy.

x = 25

    if 10 <= x and x <= 50: # <= means: less than or equal, similar >= would mean: more than or equal
        print("x is in the middle")
     # another example:

    if x < 10 or x > 50:
        print("x is not in the middle")

A neat cool trick that you can do in python is some regular mathematical inequalities:

x = 25

    if 20 <= x < 30:
        print("x is in the twenties")
    

As a side note, != means not equal.

Homework Class 2

Now that you know how to get input from the user, you will do some logic on the users input.

  1. ask the user for their name
    • if they give you name that does not have at least 3 charachters or more than 20, ask one more time.
      • if again they do not give you a good name, don't do anything more, you may use exit()
  2. If the initial of their first name is before m or m, then tell them "your first name starts at the beginning of the alphabet"
  3. Otherwise tell the user "Your first initial starts at the end of the alphabet"

Here is some pseudocode to help with your first coding homework:

get name from user
    if its less than 3 letters or longer than 20
        get name from user
        if name is still less than 3 letters or longer than 20
            exit
    if initial of first name is between 'a' and 'm'
        print("your first name starts at the beginning of the alphabet")
    else
        print("Your first initial starts at the end of the alphabet")
    

You may use the helperfunctions file. There you can find the functions between(charA, charB) use like:

'c' in between('a','d') # True
    'C' in between('a','d') # True even though it is capitalized
    'f' in between('a','d') # False

To include the between function in your code, on the top of your code write:

from helperFunctions import *

the "*" is a wildcard, like a joker in cards, it tells the computer, I want anything and everything. It is generally better to name the functions you are looking for, this way when you reread your code (or someone reads yours), it will be known why that file/library/module was called in, but for our sake, this is enough. At this stage it is not important for us to delve into the functions that were prepared.

If you would like to do it:

from helperFunctions import between

You may see this done differently online or in other examples, we will talk about importing later as well.

This reading on if/else statements from the reading section may come in handy.