I719 Fundamentals of Python/lecture2v2

From ICO wiki
Jump to navigationJump to search

Lecture 2

Dictionaries, Importing, HTTP Requests, and Classes

Dictionaries

Map keys to values

# Make a list
>>> a = [1, 2, 3]
# get element of a list using the index of an element 
>>> a[1]
2
>>> a[0]
1
# get character in a string using the index of a character
>>> hello = "hello"
>>> hello[1]
'e'
# create a dictionary, with name of a programming language as a key
# and the year created as the value
>>> example_dict = {'python': 1991}
>>> example_dict
{'python': 1991}
# get value from a dictionary using the key
>>> example_dict['python']
1991
# accessing a key that does not exist
>>> example_dict['p']
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    example_dict['p']
KeyError: 'p'
>>> example_dict.get('python')
1991
>>> example_dict.get('pythons')
>>> example_dict.get('pythons', 1990)
1990
# adding a new item (key and value pair) to a dictionary
>>> example_dict['javascript'] = 1995
>>> example_dict
{'javascript': 1995, 'python': 1991}
>>>

Example creation of dictionary

Make a dictionary mapping numbers to squares

def map_numbers_to_squares(numbers):
    result = {}
    for number in numbers:
        result[number] = number ** 2
    return result

TASK 1

Args: multiplier: a number to multiply numbers by numbers: a list of numbers

Returns: dict of numbers, mapping each number to the number multiplied by the multiplier number is the key, the product is the value

>>> test_function(4, [1,2])
# 1*4 = 4, 2*4 = 8
{1: 3, 2: 8}

Answer:

def map_products(multiplier, numbers):
    result = {}
    for number in numbers:
        result[number] = number * multiplier
    return result

Importing

import requests
import requests as r
from requests import get

Requests library

pip3 install requests

import requests

response = requests.get('http://google.com')

response.text  # get the response body as a string

response.json()  # if the response is json, convert it into python native types

TASK 2

Get the bitcoin price in euros, and print it.

Importable thing

TASK 3

make a function that returns the bitcoin price. From another file, print the price

Classes

This is a basic intro to classes. Classes organize code and provide the basis for the object/type system of python. An object has data and behavior. Classes define new objects. This python course will not require object oriented design, but you should know the basics of a python class.

# define a class
class Animal:
    # this is a magic method.
    # __init__ is called when the class is instantiated
    # `my_animal = Animal('masha')` makes a new Animal, executes this method, and assigns the new animal to 'my_animal'
    def __init__(self, name):
        # self refers to newly created instance of the class.
        print(id(self))
        # this creates an instance attribute 'name' 
        self.name = name


# define a Dog, which is Animal
# Dog also has the same __init__ method from animal
class Dog(Animal):
    def change_name(self, name):
        self.name = name


# define a Cat, which is an Animal
class Cat(Animal):
    def kak_zavut(self):
        return 'minu nimi on {}'.format(self.name)