1 of 113

Object-Oriented Programming

Prof. Seungtaek Choi

2 of 113

Recap

  • Course Overview
  • Python Basics (1)

3 of 113

Today

  • Announcement: 1st Assignment!
  • Git/GitHub Basics
  • Python Basics (2)

4 of 113

1st Assignment!

5 of 113

Git/GitHub Basics�(Or, How to Submit Assignment)

6 of 113

What is Git?

  • Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

7 of 113

What is GitHub?

  • GitHub is a cloud-based platform built on the "Git" version control system that provides tools for developers to store, manage, share, and collaborate on code and other files.

8 of 113

Git & GitHub?

  • git: version control & code management (local)
  • github: code storage (cloud)
  • push: upload code to github

push

9 of 113

Components of Git: Repository

  • Repository is a version-controlled project space that stores your files, branches, and full change history.

10 of 113

Components of Git: Branch

  • Branch is an independent line of development – a named timeline of commits within a repo.

11 of 113

Components of Git: Commit

  • Commit is a saved snapshot of changes with a message, author, and timestamp.

12 of 113

Git Branching Structure (Dev)

branch

branch

branch

branch

branch

commit

13 of 113

Git Branching Structure (Ours)

main

main

HUFS-LAI-Seungtaek/HUFS-LAI-OOP-2025-2:main

hufs-student-2/HUFS-LAI-OOP-2025-2:main

main+1

assignment1.py

main+2

assignment1.py

main+2

main+1

main

hufs-student-1/HUFS-LAI-OOP-2025-2:main

main+1

assignment1.py

14 of 113

Git Workflow

15 of 113

Git Workflow

  • Repository structure:
    • upstream (professor:main): original repo
    • origin (student:main): your repo
    • local: your computer

16 of 113

Git Workflow: fork

17 of 113

Git Workflow: clone

18 of 113

Git Workflow: add & commit

19 of 113

Git Workflow: add & commit

20 of 113

Git Workflow: add & commit

21 of 113

Git Workflow: add & commit

22 of 113

Git Workflow: add & commit

23 of 113

Git Workflow: push

24 of 113

Git Workflow: PR & merge

25 of 113

Git Workflow: branch

  • For your assignment, no need to use `branch`
    • professor:main (remote) 🡪 fork
    • student:main (remote) 🡪 clone
    • student:main (local) 🡪 commit
    • student:main+1 (local) 🡪 commit
    • student:main+2 (local) 🡪 push
    • student:main+2 (remote) 🡪 PR
    • professor:main+1 (remote)
  • It’s not about remote vs. local.
  • It’s about w/ permission vs. w/o permission.

26 of 113

Git Workflow: branch

  • In your team’s repo, if you are not allowed to push `main` branch, …
    • student:main (remote) 🡪 clone
    • student:main (local) 🡪 checkout (In this case, $ git checkout –b feature)
    • student:feature (local) 🡪 commit
    • student:feature+1 (local) 🡪 commit
    • student:feature+2 (local) 🡪 push
    • student:feature+2 (remote) 🡪 PR
    • student:main+1 (remote)

27 of 113

GitHub Web Shortcuts �(Or, How to Submit Assignment #1)

28 of 113

Fork repository

29 of 113

Fork repository

30 of 113

Repo is copied under your account.

31 of 113

Add a file

32 of 113

Add a file

33 of 113

Add a file

members/{학생이름}.md

Example: members/seungtaek.md

Example: members/yeachan.md

Example: members/gildong.md

Don’t include {}�Don’t use uppercase

Don’t use Korean

34 of 113

35 of 113

Introduce yourself

You can see actual “code” from

https://github.com/HUFS-LAI-Seungtaek/HUFS-LAI-OOP-2025-2/blob/main/members/seungtaek.md?plain=1

Feel free to introduce yourself more!

36 of 113

Commit the change (your file)

37 of 113

Back to “your” repo

38 of 113

Submit PR to lecture repository

39 of 113

Submit PR to lecture repository

40 of 113

Submit PR to lecture repository

Please follow the format �n-th Assignment by {학번} ({Full name})

Do not use or

Please use ` (look at the ~)

You can see the preview.

41 of 113

Python Basics (2)

42 of 113

Comments (1)

  • You can use “#”

43 of 113

Comments (2)

  • You can use multiple line comments with (""")

44 of 113

Data Types (1)

  • Why data types matter?
    • Define what kind of data a variable can store
    • Determine what operations can be performed
    • Affect memory usage and performance
    • Ensure data integrity in your programs

45 of 113

Data Types (2)

  • Built-in data types (not full list)
    • Text type: str
    • Numeric types: int, float
    • Sequence types: list, tuple
    • Mapping type: dict
    • Set types: set
    • Boolean type: bool
    • None type: None

46 of 113

Data Types (3)

  • Mutable
    • list
    • dict
    • set
  • Immutable
    • Text type: str
    • Numeric types: int, float
    • Sequence types:, tuple

47 of 113

Data Types (3)

  • Text type: str
    • Definition: Immutable sequence of Unicode characters
    • Common operators: + (concatenation), * (repeat), [] (index), [:] (slice)

48 of 113

Q0: “How to Change Pithon to Python?”

49 of 113

Q0: “How to Change Pithon to Python?”

A0:

50 of 113

Data Types (4)

  • Numeric types: int, float
    • Definition: Integer (int) and floating-point (float) numbers
    • Common operators: +, -, *, /, // (floor division), % (modulus), ** (power)

51 of 113

Data Types (5)

  • Sequence types: list, tuple
    • Definition: Ordered collections (list is mutable, tuple is immutable)
    • Common operators: + (concatenate), * (repeat), [] (index), [:] (slice), in (membership)

52 of 113

Q1: “Why list in tuple is changed?”

53 of 113

Q1: “Why list in tuple is changed?”

A1: “It’s about reference immutability.

54 of 113

Q2: “Why does changing y also change x?”

55 of 113

Q2: “Why does changing y also change x?”

A2: “It’s about reference copy.

56 of 113

Data Types (6)

  • Mapping type: dict
    • Definition: Key-value pairs, mutable, keys are unique
    • Common operators: in (check key), [] (access value), .keys(), .values(), .items()

The most adorable feature of Python for me.

57 of 113

Data Types (7)

  • Set type: set
    • Definition: Unordered collection of unique elements
    • Common operators: | (union), & (intersection), - (difference), ^ (symmetric difference)

58 of 113

Q3: “Why is dict + dict not allowed?”

59 of 113

Q3: “Why is dict + dict not allowed?”

A3: “Explicit is better than implicit.”

60 of 113

Be explicit (b will overwrite a)

61 of 113

Data Types (8)

  • Boolean type: bool
    • Definition: Logical values True or False
    • Common operators: and (&), or (|), not

More Pythonic

62 of 113

Data Types (9)

  • None type: None
    • Definition: Represents “no value” or “null” (= “nothing here”)
    • Common operators: is, is not

63 of 113

String Format

  • %s: string
  • %c: character (or integer interpreted as Unicode code point)
  • %d: integer (decimal)
  • %f: floating-point (decimal form)
  • %o: octal (base 8) integer
  • %x: hexadecimal (base 16) integer
  • %%: literal %

64 of 113

String Format

65 of 113

Indexing and Slicing

  • Negative indexing
    • s[-1]: last element
    • s[-2]: second last element
  • Slicing with Step
    • s[start:end:step]
    • s[::]: copy (normal order)
    • s[::2]: every second element
    • s[::-1]: reversed

66 of 113

String Built-in Functions (1)

  • Counting & Searching
    • .count(sub) 🡪 count occurrences
    • .find(sub) 🡪 return index, -1 if not found
    • .index(sub) 🡪 return index, error if not found

67 of 113

String Built-in Functions (2)

  • Transformation
    • .upper() / .lower() 🡪 change case (new string)
    • .replace(old, new) 🡪 replace substring
    • .join(iterable) 🡪 insert string between elements

68 of 113

String Built-in Functions (3)

  • Whitespace Handling & Splitting
    • .lstrip() / .rstrip() / .strip() 🡪 remove spaces
    • .split([sep]) 🡪 split into list

69 of 113

String Built-in Functions (4)

  • Checks
    • .isalpha() 🡪 only letters?
    • .isdigit() 🡪 only digits?
    • .startswith(sub) / .endswith()

70 of 113

Useful List Functions (1)

  • Functions
    • .append() 🡪 add element at end
    • .sort() 🡪 sort ascending
    • .reverse() 🡪 reverse order
    • .index(x) 🡪 return index of first occurrence

71 of 113

Useful List Functions (2)

  • Functions
    • .insert(i, x) 🡪 insert at position
    • .remove(x) 🡪 remove first occurrence
    • .pop([i]) 🡪 pop element (last or at index)
    • .count(x) 🡪 count occurrences
    • .extend(list) 🡪 extend with another list

72 of 113

if Statements

  • Programs make decisions based on conditions.
  • Indentation is part of the syntax (use 4 spaces; do not mix tabs/spaces).
  • A colon : follows if / elif / else.

73 of 113

if Statements

  • Logical operators & short-circuit
  • and, or, not (short-circuit evaluation)

74 of 113

if Statements

  • elif for multiple branches

75 of 113

if Statements

  • Math-style chaining improves readability

76 of 113

if Statements

  • Conditional expression (ternary)
  • Form: A if condition else B

77 of 113

if Statements

  • Do nothing with pass

78 of 113

if Statements

  • Blank list is falsy (not actually False).

79 of 113

while Loops

  • Repeats block while condition is True.
  • Ends when condition becomes False.

80 of 113

while Loops

  • break exists loop immediately.

81 of 113

while Loops

  • continue jumps back to condition check.

82 of 113

while Loops

  • else executes only if loop ended normally (not by break).

83 of 113

for Loops

  • Iterates directly over elements of a sequence (list, tuple, string, etc.).
  • No need for index counters unless explicitly needed.

84 of 113

for Loops

  • enumerate returns index + value.

85 of 113

for Loops

  • zip iterates multiple lists together.

86 of 113

for Loops

  • continue skips the rest of the loop body and jumps to the next iteration.

87 of 113

for Loops

  • range generates arithmetic sequences.

88 of 113

for Loops

  • Example of nested loops (multiplication table)

89 of 113

for Loops

  • break exists loop immediately.
  • else runs only if loop terminates normally (no break).

90 of 113

List Comprehensions

  • Concise syntax to build lists.

91 of 113

Function

  • A function maps inputs to an output after performing a task.
  • Why?
    • Reuse: avoid repeating code
    • Clarity: structure programs by tasks
    • Testability: small, composable units

92 of 113

Function

  • Syntax
    • def + name + parameters
    • return sends the result (and exits the function)

93 of 113

Function

  • Parameters vs. Arguments
    • Parameters: variables in the function definition
    • Arguments: actual values passed at call site

94 of 113

Function

  • Four shapes (inputs/return)

95 of 113

Function

  • Calling with keywords
    • Call by name, order doesn’t matter

96 of 113

Function

  • Variable-length arguments
    • *args: collect extra positional args (tuple)
    • **kwargs: collect keyword args (dict)

97 of 113

Function

  • Function returns always one object
    • You can return multiple values via a tuple
    • First return encountered ends the function.

98 of 113

Function

  • Default parameters (and order)
    • Defaults must come after non-defaults

99 of 113

Function

  • Scope & side effects
    • Parameters are local to the function

100 of 113

Function

  • Scope & side effects
    • Prefer returning the new value

101 of 113

Function

  • Scope & side effects
    • global exists but avoid (hurts modularity)

102 of 113

Function

  • Mutable objects can be modified in place.

103 of 113

Function

  • lambda: one-line anonymous functions
    • It is great as small callbacks / key functions.

104 of 113

Function

  • Document your function right under the def

105 of 113

Function

  • Early return to exit on special cases

106 of 113

Function

  • Combine *args with a mode

107 of 113

User & File I/O

  • User I/O: input(), print()
  • File I/O: open(), read/write, close()
  • Why important?
    • Most programs take input & product output
    • Need persistence beyond one run 🡪 files

108 of 113

User & File I/O

  • User input/output
    • input() 🡪 always string, convert with int(), float()
    • print() 🡪 sep, end customize formatting

109 of 113

User & File I/O

  • Basic file writing/reading
    • Modes: ‘r’ read, ‘w’ write (overwrite), ‘a’ append
    • encoding=“utf-8”: avoid mojibake (한글 깨짐 방지)

110 of 113

User & File I/O

  • Reading variants (memory vs. streaming)
    • reads() loads all 🡪 risky on huge files
    • Iteration (for line in f) reads chunk by chunk 🡪 scalable

111 of 113

User & File I/O

  • Why with matters
    • Auto-close file even if error occurs
    • Common pitfalls without with:
      • Forgetting close() 🡪 locked file, lost data
      • Writing without encoding 🡪 OS-dependent behavior
    • With-block != new scope (Python has function scope, not block scope)

112 of 113

Error Handling

  • Typical runtime errors in I/O:
    • ValueError
    • ZeroDivisionError
    • FileNotFoundError, UnicodeDecoderError

113 of 113

Next

  • Python Advanced
  • OOP Basics