I719 Fundamentals of Python/lecture1

From ICO wiki

Lesson 1

python 2 or 3?

  • v2 is more commonly used
  • v3 is the official standard
  • we are using python 2 today
  • all the code we write today will run on both versions
  • We will use python 3 next class

Language basics

interactive

many interactive shells

  • default repl, python
  • ipython
  • bpython

Whitespace dependent

  • 4 space indent
  • no brackets { } like in java
  • no semicolons

Comments

  • use # for comments

Types

  • to find the type of a symbol a, use type(a)

Strings

  • double or single quotes are fine
  • length of string determined by function len()

functions

  • to define a function, start with def
  • then the function name
  • functions are always in snake case. always lower case and words are separated by underscores
  • add the end of the name, list of arguments in parethesis
  • to document function, use triple quoted string after function definition
  • functions can use named arguments, which allow a default value for the argument. They are called 'Keyword Arguments'
  • Arguments can be made in order they are in the function definition, or be named specifically

Arithmetic

similar to java, 1 + 1 returns 2

Lists

lists in Python are like arrays in Java.

a = [1, 2, 3]  # defines a list

For loops

Python's for is different from other languages. It is a "foreach" loop. It goes through each element of an iterator.

"""
0
1
2
"""
for i in range(3):
    print(i)

Importing and Libraries

  • the standard library of python is giant

importing random to run randint()

import random
random.randint(1, 10)

or

from random import randint
randint(1, 10)

or to name the import something else

from random import randint as randominteger

Classes

  • classes are made with the keyword 'class'
  • class names are CamelCase

Methods

  • the __init__ method is ran on instantiation
  • first argument of a normal method must always be self
  • self is the instance created by instantation
  • __init__ and __str__ are "magic methods"
class Car:  # define the class with a name
    def __init__(self, name, wheels): # defines a method
    """
    This method is ran on each instantiation of this class
    example, `Car('hi')` will run this method
    """
    self._name = name  # assigns a property to the instance
        self.wheels = wheels

    def __str__(self):
        """
        Give the instance a name. Is called during `print(instance)`
        """
        return "{0} with {1} wheels".format(self.name, self.wheels)

    def print_wheels(self):
        print(self.wheels)

c = Car('Wagon', 10) # make an object from a class, ie Instantiation

print(c)  # when print a instance, the __str__ method is called
c.print_wheels()

Tasks

Task 1

print 10 lines, each printing 'hello $N', where N is the line number

Example Solution

"""
Task 1
"""

def add_number_to_hello(n=1):
    """
    Takes a number, and adds it to the
    end of string 'hello '
    """
    result = 'hello {0}'.format(n)
    return result

for i in range(1, 11):
    print(add_number_to_hello(n=i))

Task 2

"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."

Example Solution

for x in range(1,101):
    if x % 15 == 0:
        print "FizzBuzz" # Catch multiples of both first.
    elif x % 3 == 0:
        print "Fizz"
    elif x % 5 == 0:
        print "Buzz"
    else:
        print x