Python 102


Task 1 - Read E-book

  1. Download e-book
  2. Read Chapter 5
    1. Practice what you read
    2. Watch The Video Lesson
    3. Chapter 5 Quiz
  3. Read Chapter 6
    1. Practice what you read
    2. Watch The Video Lesson
    3. Chapter 6 Quiz
  4. Read Chapter 7
    1. Practice what you read
    2. Watch The Video Lesson
    3. Chapter 7 Quiz
  5. Read Chapter 8
    1. Practice what you read
    2. Watch The Video Lesson
    3. Chapter 8 Quiz

Task 2 - Follow these coding exercises

Please try the following coding examples yourself.

Python Control Flow


Indentation

Python Uses Indentation to do Control Flow.
It is important to keep a good understanding of how indentation works in Python to maintain the structure and order of your code.

The Three Ways

In this universe, anything can be done by these three ways:

  1. Step by Step: line by line
  2. Branch: if… elif… else..
  3. Looping: for, while

Branch: if… elif… else..

The if Statement in Python allows us to tell the computer to perform alternative actions based on a certain set of results.

Verbally, we can imagine we are telling the computer: "Hey if this case happens, perform some action"

We can then expand the idea further with elif and else statements, which allow us to tell the computer:
"Hey if this case happens, perform some action. Else if another case happens, perform some other action. Else-- none of the above cases happened, perform this action"

Let's go ahead and look at the syntax format for if statements to get a better idea of this:
    if case1:
        perform action1
    elif case2:
        perform action2
    else:
        perform action 3
                

Let's see a quick example of this:

    >>> x = int(input("Please enter an integer: "))
    Please enter an integer: 42
    >>> if x < 0:
    ...     x = 0
    ...     print('Negative changed to zero')
    ... elif x == 0:
    ...     print('Zero')
    ... elif x == 1:
    ...     print('Single')
    ... else:
    ...     print('More')
    ...
    More
                

Looping: for, while

The for statement in Python is used to do repeating.
Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

    >>> # Measure some strings:
    ... words = ['cat', 'window', 'defenestrate']
    >>> for w in words:
    ...     print(w, len(w))
    ...
    cat 3
    window 6
    defenestrate 12
                

Python Function

Functions will be one of our main building blocks when we construct larger and larger amounts of code to solve problems.

What is a function?

Formally, a function is a useful device that groups together a set of statements so they can be run more than once. They can also let us specify parameters that can serve as inputs to the functions. On a more fundamental level, functions allow us to not have to repeatedly write the same code again and again.

There are two kinds of functions: built-in functions, which are already created by python, and User defined functions (UDF)

For example, the function len(), which is a built-in function, is used to get the length of a string or a list. Since checking the length of a sequence is a common task you would want to write a function that can do this repeatedly at command. Function is one of most basic levels of reusing code in Python.

Function Syntax

  1. Indentation and the def Statement are used to build out a function in Python.
  2. A list of parameters in (x, y, z). It is possible for a function to not have any parameters just like (). Do not forget the ':', at the end of the first line of the function definition.
  3. Use a return statement, a function will return a result that can then be stored as a variable, or used in whatever manner a user wants.

Here is a basic example

    >>> def add_num(num1,num2):
    ...     return num1+num2
    >>> add_num(4,5)
    9
                

Here is a UDF function, it tests if a give number is prime:

    >>> def is_prime(num):
            '''
            Naive method of checking for primes.
            '''
            for n in range(2,num):
                if num % n == 0:
                    print ('not prime')
                    break
            else: # If never mod zero, then prime
                print ('prime')
    >>> is_prime(25)
    not prime
                

Task 3 - Coding Questions


You will be turing in this assignment to you google classroom. Please save your 5 functions to one .py file demark the question numbers and the question in a comment above it's respective function

Question 1

Write a function to print "hello_USERNAME!" USERNAME is the input of the function. The first line of the code has been defined as below.

    def hello_name(user_name):
        .....
                

Question 2

Write a python function, first_odds that prints the odd numbers from 1-100 and returns nothing

    def first_odds():
        .....
                

Question 3

Please write a Python function, max_num_in_list to return the max number of a given list. The first line of the code has been defined as below.

    def max_num_in_list(a_list):
        .....
                

Question 4

Write a function to return if the given year is a leap year. A leap year is divisible by 4, but not divisible by 100, unless it is also divisible by 400. The return should be boolean Type (true/false).

    def is_leap_year(a_year):
        .....
                

Question 5

Write a function to check to see if all numbers in list are consecutive numbers. For example, [2,3,4,5,6,7] are consecutive numbers, but [1,2,4,5] are not consecutive numbers. The return should be boolean Type.

    def is_consecutive(a_list):
        .....
                
Continue to Html_CSS