What are our goals?: Learn Python syntax and features Understand - - PowerPoint PPT Presentation

what are our goals
SMART_READER_LITE
LIVE PREVIEW

What are our goals?: Learn Python syntax and features Understand - - PowerPoint PPT Presentation

presents Intermediate Python What are our goals?: Learn Python syntax and features Understand why Python is so powerful and popular for so many different uses 2 Why are doing this?: Because were nice people, who


slide-1
SLIDE 1

Intermediate Python

presents

slide-2
SLIDE 2

2

What are our goals?:

  • Learn Python syntax and features
  • Understand why Python is so

powerful and popular for so many different uses

slide-3
SLIDE 3

3

Why are doing this?:

Because we’re nice people, who want to provide others with the same opportunity to learn to code well in the same way we were

slide-4
SLIDE 4

4

Who am I?:

  • Jack
  • 3rd year JMC
  • DoCSoc Academic Events

Director

  • Worked as Software

Engineer / Data Science Intern before, going to JPMorgan Tech this summer

slide-5
SLIDE 5

What concepts should you know already?

5

  • Data types and structures
  • Control flow (If statements / conditionals)
  • Iteration (Loops)
  • Functions

Don’t worry if you’re rusty, we’ll do a recap!

slide-6
SLIDE 6

What will you learn?

6

  • Python syntax and features
  • Object-Oriented Programming and good practices
  • Scientific Computation Libraries such as Pandas, Numpy
  • Applications of Python:

○ Statistics ○ Sciences ○ Machine Learning / Data Science ○ Web Servers, tools, etc

  • Beyond the first/second week, you can vote on what’s next
slide-7
SLIDE 7

How will you learn?

7

  • Lecture (Theory) Content
  • Demos, Live Coding
  • Use both Interpreter and write Python programs
  • Exercises to do
  • Menti Quizzes
  • Questions are encouraged + Google yourself!
slide-8
SLIDE 8

What will we need?

8

  • A laptop with

○ Python 3.x (preferably 3.6 or 3.7) ○ A text editor

  • Some dedication and effort!
slide-9
SLIDE 9

Some FAQ before we begin

9

  • I’ve done ___ before, but not Python?
  • I know concept X but not sure about Y?
  • Can I use an IDE?
  • What the hell is going on in the demo?
slide-10
SLIDE 10

And lastly…...

10

slide-11
SLIDE 11

A little bit about yourselves :)

11

slide-12
SLIDE 12

Let’s begin!

12

slide-13
SLIDE 13

We’re going to begin with a recap of all the basics / as a crash course for those

  • f you coming from other programming

languages

13

slide-14
SLIDE 14

Basic Data Types

14

What are some basic data types?

slide-15
SLIDE 15

Basic Data Types

15

  • Integer
  • Float/Double
  • Char
  • String
  • Bool
  • Null
slide-16
SLIDE 16

Basic Data Types

16

x = 5 y = 8 samplebool = True message = "This is a string!" z = 5.5

slide-17
SLIDE 17

Basic Data Types

17

int x = 0; x = x + 5; int y; y = age 10; string message; message = “A message”;

In other languages, you need to declare the type of the variable first. This is not the case for Python, as it is dynamically typed.

slide-18
SLIDE 18

Dynamic Typing AKA Duck Typing

18

The type of variables are not checked until they are used in the program, at which either it works because it’s the correct type, or all hell breaks loose because it’s not

slide-19
SLIDE 19

Basic Data Types

19

x = 5 y = 2 z = 3.0 x + y x + 2.2 x / y 10 / 2

What are the types of these (if they work at all)?

slide-20
SLIDE 20

Basic Data Types

20

x = 5 y = 2 z = 3.0 x + y x + 2.2 x / y 10 / 2

What are the types of these (if they work at all)?

slide-21
SLIDE 21

Type Casting

21

(Attempting to) Force a variable into another type

slide-22
SLIDE 22

Basic Data Types

22

True and False True or False False + False False - False What are the types of these (if they work at all)?

slide-23
SLIDE 23

Basic Data Types

23

True and False True or False False + False False - False What are the types of these (if they work at all)? What happens when you try casting between ints, floats, and bools?

slide-24
SLIDE 24

Basic Data Types

24

"message1" + "message2" "message1" - "message2" "message1" + ‘message2’ "message1" + 5 "message1" + True "message1" * 3 "c" ‘c’ What are the types of these (if they work at all)?

slide-25
SLIDE 25

Basic Data Types

25

"message1" + "message2" "message1" - "message2" "message1" + ‘message2’ "message1" + 5 "message1" + True "message1" * 3 "c" ‘c’ What are the types of these (if they work at all)? Play around with casting these

slide-26
SLIDE 26

Basic Data Types

26

  • Number (Integer or Float)
  • String
  • Char
  • Bool
  • Null (None)
slide-27
SLIDE 27

Python Data Structures

27

  • Lists
  • Dictionary
  • Set
slide-28
SLIDE 28

Lists

28

  • Similar to arrays in other languages
  • No fixed size, dynamically grows
  • 0-indexed
  • Item can be different types
  • Can be nested, and “sliced”
slide-29
SLIDE 29

Lists

29

l = [1,2,3,4] l.append(5) l.append("Six") l.append(True) l[2] l[0:4]

slide-30
SLIDE 30

Generating Lists

30

list_1 = list(range(5)) list_1 = [i for i in range(5)] list_2 = list(range(5, 10)) list_2 = [i for i in range(5,10)] summed_list = [x + y for x, y in zip(list_1, list_2)]

slide-31
SLIDE 31

Generating Lists

31

list_1 = list(range(5)) list_1 = [i for i in range(5)] list_2 = list(range(5, 10)) list_2 = [i for i in range(5,10)] summed_list = [x + y for x, y in zip(list_1, list_2)]

Check out the actual documentation as well

slide-32
SLIDE 32

Dictionaries

32

  • Similar to (Hash)Maps in other languages
  • Maps Keys to Values
  • Not type-aware
  • You can have dictionaries within dictionaries

(within dictionaries…..)

  • Similar to JSON
slide-33
SLIDE 33

Dictionaries

33

released = { "iphone" : 2007, "iphone 3G" : 2008, "iphone 3GS" : 2009, "iphone 4" : 2010, "iphone 4S" : 2011, "iphone 5" : 2012 } released["iphone"]

Check out the actual documentation as well

slide-34
SLIDE 34

Sets

34

  • Pretty much the same as other languages
  • Similar to lists. but

○ Duplicates are removed ○ No order

  • Not type aware
slide-35
SLIDE 35

Sets

35

myset = {1,2,3,4,5} myset.add("Six") Check out the actual documentation as well

slide-36
SLIDE 36

Warning: Copying/Modifying structures/objects

36

“Objects” are referred to by reference, so always check what you are doing

slide-37
SLIDE 37

Warning Example

37

l1 = [1,2,3,4,5] l2 = l1 l2.append(6)

slide-38
SLIDE 38

Control Flow

38

  • If/else statements
  • Loops
slide-39
SLIDE 39

If statements

39

if True: print("Yay!")

slide-40
SLIDE 40

If statements

40

if True: print("Yay!") elif False: print("Boooohooo")

slide-41
SLIDE 41

If statements

41

if 1: print("Yay!") elif 0: print("Boooohooo") else: print("What's going on?")

The condition doesn’t have to be a bool itself!

slide-42
SLIDE 42

While Loops

42

while True: print("This will go on forever!") While a certain condition is true, do something

slide-43
SLIDE 43

While Loops

43

i = 0 while i < 10: print(i) i += 1

While a certain condition is true, do something

slide-44
SLIDE 44

While Loops

44

print("Marco....") user_input = input() while user_input != "Polo": print("Wrong Answer! Try Again!") print("Marco....") user_input = input() print("Yay!")

slide-45
SLIDE 45

While Loops

45

print("Marco....") while input() != "Polo": print("Wrong Answer! Try Again!") print("Marco....") print("Yay!")

slide-46
SLIDE 46

For loops

46

for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } In other languages:

slide-47
SLIDE 47

For loops in Python:

47

“For an element x in <something>, do <this>”

slide-48
SLIDE 48

For loops

48

for x in [1,2,3,4,5]: print(x) for name in ["John", "Paul", "George", "Ringo"]: print(name + " is in the Beatles.") for i in range(0,10): print(i)

slide-49
SLIDE 49

For loops

49

for x in [1,2,3,4,5]: print(x) for name in ["John", "Paul", "George", "Ringo"]: print(name + " is in the Beatles.") for i in range(0,10): print(i)

Note: a for loop doesn’t have to take in a list, it can take in any iterator

slide-50
SLIDE 50

Quick Exercise:

50

Using either a For or While loop, print the first 10 multiples of 5

slide-51
SLIDE 51

Functions

51

  • Abstract code away
  • Takes input (maybe)
  • Does something (maybe)
  • Returns output (maybe)
slide-52
SLIDE 52

Functions example

52

beatles = ["John", "Paul", "George", "Ringo"] def greet(name): print("Hello " + name + ", how are you doing?") for beatle in beatles: greet(beatle)

slide-53
SLIDE 53

Functions

53

  • Variables created inside are forgotten once the function

is finished executing

  • Python allows for “default arguments”
  • Does not have to consistently return (or not return) the

same type

  • Can call itself (recursion)
slide-54
SLIDE 54

More function examples

54

def greet(name=None): if name is None: name = input() print("Hello " + name + ", how are you doing today?") def fib(n): if n == 0 or n == 1: return 1 else: return fib(n - 1) + fib(n - 2)

slide-55
SLIDE 55

Quick Exercise:

55

Write a function that given a positive integer n, returns the sum of all the numbers from 1 to n

slide-56
SLIDE 56

Questions so far?

56

slide-57
SLIDE 57

Break time!

57

slide-58
SLIDE 58

Objects and Classes

58

slide-59
SLIDE 59

What objects/classes do we already know?

59

slide-60
SLIDE 60

Objects/Classes

60

  • Encapsulates data, and functions centered around them

(methods)

  • Reduces code duplication
  • With good design, can be reused, extended etc.
  • Allows a “program” to be split into smaller components:

○ Easier to think about and write ○ Work can be split up easily

slide-61
SLIDE 61

Example 1

61

  • You want a class to represent a Person

○ What data do you want to have about a Person? ○ What methods should a Person be capable of?

slide-62
SLIDE 62

Example 1

62

  • You want a class to represent a Person

○ What data do you want to have about a Person? ■ Firstname, Lastname, Age…...etc. ○ What methods should a Person be capable of? ■ Greet, Eat, Grow…..etc.

slide-63
SLIDE 63

Example 1

63

By writing Person class, you can now instantiate as many Person objects as you want

slide-64
SLIDE 64

Person Class

64

class Person: def __init__(self, name): self.name = name

slide-65
SLIDE 65

Person Class

65

class Person: def __init__(self, name): self.name = name def greet(self): print("Hello! My name is", self.name)

slide-66
SLIDE 66

Person Class

66

john = Person("John") john.greet()

slide-67
SLIDE 67

Person Class

67

class Person: def __init__(self, name): self.name = name def greet(self): print("Hello! My name is", self.name)

Constructor Field Method

slide-68
SLIDE 68

Class-level Properties

68

class Person: race = "Human" def __init__(self, name): self.name = name

slide-69
SLIDE 69

Example 1

69

What else can we add to the Person class?

slide-70
SLIDE 70

Default Object Methods

70

All Objects have default methods for certain tasks, for example, print(a) will call a.__str__(). See documentation for more details.

slide-71
SLIDE 71

Person class

71

  • We created a single class to encapsulate a Person, so we

can create multiple people.

  • Each Person object

○ can be created simply and easily ○ has the same functionality

  • You should now begin to see why Object-Oriented

Programming is useful

slide-72
SLIDE 72

Example 2

72

  • You want a simple, command line rock-paper-scissors

game

  • How would you do this?
slide-73
SLIDE 73

Example 2

73

  • You want a simple, command line rock-paper-scissors

game

  • How would you do this?

○ Option 1: Create a RockPaperScissors class to encapsulate the game

slide-74
SLIDE 74

Example 2

74

  • You want a simple, command line rock-paper-scissors

game

  • How would you do this?

○ Option 1: Create a RockPaperScissors class to encapsulate the game ○ If you were to host a board games night, what would you need?

slide-75
SLIDE 75

Example 2

75

  • You want a simple, command line rock-paper-scissors

game

  • How would you do this?

○ Option 2: Create a RockPaperScissors game class, as well as a Player class. ○ This allows us to separate (and possibly later reuse) the logic

slide-76
SLIDE 76

Example 2

76

  • You want a simple, command line rock-paper-scissors

game

  • How would you do this?

○ Option 2: Create a RockPaperScissors game class, as well as a Player class. ○ This allows us to separate (and possibly later reuse) the logic

How should do the two classes interact with each other now?

slide-77
SLIDE 77

Interfaces (AKA an API)

77

A set of rules which define how a component should interact with another

slide-78
SLIDE 78

Interfaces

78

  • A very general programming concept - not Python specific

○ Other languages have features to enforce an interface ○ You will also hear about Web APIs

  • An interface isn’t good or bad by itself - it depends on the

context and use cases

  • Abstracts away the usage from the implementation
  • “Design” of a program/application
slide-79
SLIDE 79

Our game interface

79

RockPaperScissors class:

  • moves() method to give a list of

possible moves (Rock, Paper, Scissors)

  • play() method to play the game, and

prints out the winner

slide-80
SLIDE 80

Our game interface

80

RockPaperScissors class:

  • moves() method to give a list of

possible moves (Rock, Paper, Scissors)

  • play() method to play the game, and

prints out the winner

HumanPlayer class:

  • pick_action() method to select a

move to play

slide-81
SLIDE 81

Our game interface

81

RockPaperScissors class:

  • moves() method to give a list of

possible moves (Rock, Paper, Scissors)

  • play() method to play the game, and

prints out the winner

HumanPlayer class:

  • pick_action() method to select a

move to play Once this has been agreed, we can now get started!

slide-82
SLIDE 82

82

What if we want to add an AI Player class?

slide-83
SLIDE 83

What design decisions have we made?

83

  • Think about type signatures of methods
  • How we create each object and use them
  • Would we change our design if our use

case was different?

slide-84
SLIDE 84

What design decisions have we made?

84

  • Think about type signatures of methods
  • How we create each object and use them
  • Would we change our design if our use

case was different?

  • What bad decisions could we have made?
slide-85
SLIDE 85

85

What if we want a Tic-Tac-Toe game now? What could we reuse? What would we need to add/change?

slide-86
SLIDE 86

That’s it for this week!

86

  • docsoc.co.uk/education for all resources
  • Next week(?), we will look at

○ a little more into classes (inheritance) ○ exceptions ○ importing libraries and project structure ○ numpy, and pandas, graph plotting, etc

slide-87
SLIDE 87

87

Also, if you’re interested....