Python Crash Course: A Comprehensive Introduction to Programming in Python, Slides of Web Programming and Technologies

→ Indentation is a requirement in Python! • Structures that introduce blocks end with a colon “:” from math import sqrt my_list = ...

Typology: Slides

2022/2023

Uploaded on 03/01/2023

mathieu
mathieu 🇮🇹

4.2

(11)

235 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DATABASE
SYSTEMS
GROUP
Python Crash Course
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Python Crash Course: A Comprehensive Introduction to Programming in Python and more Slides Web Programming and Technologies in PDF only on Docsity!

DATABASE SYSTEMS GROUP

DATABASE SYSTEMS GROUP

General

  • Conceived in the late 1980s by Guido van Rossum at CWI

in the Netherlands

  • Successor to the ABC language
    • Improvement: small core language with a large standard library

and easily extensible

  • Multi-paradigm programming language
    • Object-oriented
    • Structured
    • Functional

DATABASE SYSTEMS GROUP

Built-in Data Types: Numbers and

Booleans

  • Integer:
    • Normal Integer, e.g. i = 345
    • Octal Literals, e.g. i = 0o
    • Hexadecimal Literals, e.g. i = 0x1F
    • Binary Literals, e.g. i = 0b
  • Floating-point numbers
    • E.g. i = 1.234e-
  • Complex numbers
    • Composed of + j, e.g. i = 3+4j
  • Boolean Values
  • True and False

DATABASE SYSTEMS GROUP

Built-in Data Types: String

  • Strings are sequences of Unicode characters
  • Marked by quotes:
    • Single-quote, e.g. ‘Hello, World!’
    • Double-quote, e.g. “Hello, World!”
    • Triple-quote, e.g. ‘‘‘Hello, “World”!’’’
  • Access:

    s[2] 'N' M U N I C H 0 1 2 3 4 5 -6 -5 -4 -3 -2 - s = ' ' s[-1] 'H' s[2:] 'NICH' s[:-2] 'MUNI' s[2:-2] 'NI'

DATABASE SYSTEMS GROUP

Data Structures: Python Lists

[0b100, ['times', True ], 'is', 4.]

  • Related to Java or C arrays, BUT more powerful
  • List items do not need to have the same type
  • Lists can grow dynamically
  • Lists are ordered
  • Lists are mutable and elements can be accessed by their index

DATABASE SYSTEMS GROUP

Data Structures: Python Lists (cont.)

  • Lists (resp. Iterables) are supported by many built-in functions
    • sum()
    • len()
    • max(), min()
  • List comprehension as an elegant way to create lists

a = [x2 for x in range(7)] a [0, 1, 4, 9, 16, 25, 36] sum(a) 91 a + [x2 for x in range(7,9)] [0, 1, 4, 9, 16, 25, 36, 49, 64] del a[:3] [9, 16, 25, 36, 49, 64]

DATABASE SYSTEMS GROUP

Excursus: Copying in Python (cont.)

a = ['one',['one'‚'two']] b = a[:] print(id(a),id(b)) 85992520 85995336 b[0] = 'three‚ >>> b[1][1] = 'three' print(id(a),id(b)) >>> print(id(a),id(b)) 85992520 85995336 85992520 85995336 one two

a

No Side Effect

Shallow Copy

one sublist

b^ one

sublist one two

a

one sublist

b^ three

sublist

Side Effect

one three

a

one sublist

b^ one

sublist Solution: the method deepcopy from the module copy

from copy import deepcopy b = deepcopy(a)

DATABASE SYSTEMS GROUP

Data Structures: Tuples

('A tuple with', 3, 'entries')

  • A tuple is a sequence of comma separated values
  • Values can have different types
  • Tuples are immutable (but can contain mutable values)

t = 1, [2], 'tuple' #tuple packing t[2] 'tuple' t[0] = 3 TypeError t[1][0] = 3 t (1, [3], 'tuple') x, y, z = t #sequence unpacking

DATABASE SYSTEMS GROUP

Data Structures: Dictionaries (cont.)

  • Each key must be unique, since values are obtainable via the key
  • Dictionaries also support comprehension

d = { i2: i for i in range(7) } d {0: 0, 1: 1, 4: 2, 9: 3, 16: 4, 25: 5, 36: 6} d[4] 2 for entry in d.items(): if entry[0] == 4: print(entry) (4,2) [key for key in d.keys()] #iterating over values is supported, too [0, 1, 4, 9, 16, 25, 36] d[49] = 7 #delete values by using del key word, e.g. del d[36] d print(id(a),id(b)) 85992520 85995336 85992520 85995336 one two ##### a #### No Side Effect #### Shallow Copy one sublist ##### b^ one sublist one two ##### a one sublist ##### b^ three sublist #### Side Effect one three ##### a one sublist ##### b^ one sublist Solution: the method deepcopy from the module copy >>> from copy import deepcopy >>> b = deepcopy(a) DATABASE SYSTEMS GROUP ## Data Structures: Tuples ## ('A tuple with', 3, 'entries') - A tuple is a sequence of comma separated values - Values can have different types - Tuples are immutable (but can contain mutable values) >>> t = 1, [2], 'tuple' #tuple packing >>> t[2] 'tuple' >>> t[0] = 3 TypeError >>> t[1][0] = 3 >>> t (1, [3], 'tuple') >>> x, y, z = t #sequence unpacking DATABASE SYSTEMS GROUP ## Data Structures: Dictionaries (cont.) - Each key must be unique, since values are obtainable via the key - Dictionaries also support comprehension >>> d = { i2: i for i in range(7) } >>> d {0: 0, 1: 1, 4: 2, 9: 3, 16: 4, 25: 5, 36: 6} >>> d[4] 2 >>> for entry in d.items(): if entry[0] == 4: print(entry) (4,2) >>> [key for key in d.keys()] #iterating over values is supported, too [0, 1, 4, 9, 16, 25, 36] >>> d[49] = 7 #delete values by using del key word, e.g. del d[36] >>> d {0: 0, 1: 1, 4: 2, 49: 7, 9: 3, 16: 4, 25: 5, 36: 6}

DATABASE SYSTEMS GROUP

Conditional Statements and Loops

  • Conditional Statements:

if : <block 1> elif :

<block 2> else : <block 3>

a = 1 if (b > 2) else 0

  • Loops:

while : <block 1> else : #else case can be avoided by using break or simply be omitted <block 2> for in : <block 1> else : <block 2>

DATABASE SYSTEMS GROUP

map() , filter() and reduce()

  • map(func,seq)

def feet_to_meter(x): return x0. feet = [5.92, 49000, 1066.3] list(map(feet_to_meter, feet)) [1.804416, 14935.2, 325.00824] list(map( lambda x: x0.3048, feet)) [1.804416, 14935.2, 325.00824]

  • filter(func,seq)

list(filter( lambda x: x%2 == 1, [1,2,3,4,5])) [1, 3, 5]

  • reduce(func,seq)

from functools import reduce reduce( lambda x,y: x+y, [1,2,3,4,5]) 15

5.920.3048 490000.3048 1066.3*0.

map

True False^ True False True

filter

plus(plus(plus(plus(1,2),3),4),5)

reduce

DATABASE SYSTEMS GROUP

NumPy

  • The fundamental package for scientific computing and core part of the

SciPy stack

  • Homogeneous multidimensional arrays as main objects
  • Provides many arithmetic operations on arrays

import numpy as np A = np.array([[1,2],[1,1]]) A array([[1, 2], [1, 1]]) B = np.array([[0,1],[2,1]]) AB : <block 1> elif : <block 2> else : <block 3> >>> a = 1 if (b > 2) else 0 - Loops: >>> while : <block 1> else : #else case can be avoided by using break or simply be omitted <block 2> >>> for in : <block 1> else : <block 2> DATABASE SYSTEMS GROUP ## map() , filter() and reduce() - map(func,seq) >>> def feet_to_meter(x): return x0. >>> feet = [5.92, 49000, 1066.3] >>> list(map(feet_to_meter, feet)) [1.804416, 14935.2, 325.00824] >>> list(map( lambda x: x0.3048, feet)) [1.804416, 14935.2, 325.00824] - filter(func,seq) >>> list(filter( lambda x: x%2 == 1, [1,2,3,4,5])) [1, 3, 5] - reduce(func,seq) >>> from functools import reduce >>> reduce( lambda x,y: x+y, [1,2,3,4,5]) 15 5.920.3048 490000.3048 1066.30. #### map True False^ True False True #### filter plus(plus(plus(plus(1,2),3),4),5) #### reduce DATABASE SYSTEMS GROUP ## NumPy - The fundamental package for scientific computing and core part of the #### SciPy stack - Homogeneous multidimensional arrays as main objects - Provides many arithmetic operations on arrays >>> import numpy as np >>> A = np.array([[1,2],[1,1]]) >>> A array([[1, 2], [1, 1]]) >>> B = np.array([[0,1],[2,1]]) >>> A*B array([[0, 2],

elif : <block 2> else : <block 3> >>> a = 1 if (b > 2) else 0 - Loops: >>> while : <block 1> else : #else case can be avoided by using break or simply be omitted <block 2> >>> for in : <block 1> else : <block 2> DATABASE SYSTEMS GROUP ## map() , filter() and reduce() - map(func,seq) >>> def feet_to_meter(x): return x0. >>> feet = [5.92, 49000, 1066.3] >>> list(map(feet_to_meter, feet)) [1.804416, 14935.2, 325.00824] >>> list(map( lambda x: x0.3048, feet)) [1.804416, 14935.2, 325.00824] - filter(func,seq) >>> list(filter( lambda x: x%2 == 1, [1,2,3,4,5])) [1, 3, 5] - reduce(func,seq) >>> from functools import reduce >>> reduce( lambda x,y: x+y, [1,2,3,4,5]) 15 5.920.3048 490000.3048 1066.30. #### map True False^ True False True #### filter plus(plus(plus(plus(1,2),3),4),5) #### reduce DATABASE SYSTEMS GROUP ## NumPy - The fundamental package for scientific computing and core part of the #### SciPy stack - Homogeneous multidimensional arrays as main objects - Provides many arithmetic operations on arrays >>> import numpy as np >>> A = np.array([[1,2],[1,1]]) >>> A array([[1, 2], [1, 1]]) >>> B = np.array([[0,1],[2,1]]) >>> AB array([[0, 2], [2, 1]])

np.dot(A,B) array([[4, 3], [2, 2]])