0% found this document useful (0 votes)
2 views

[Lecture 3] Strings and Control structures

The document covers fundamental programming concepts including string types, built-in operations on strings, and control structures such as Boolean types and if/else statements. It emphasizes the importance of handling various data types and provides examples of string manipulation, input/output operations, and decision-making in programming. The content is aimed at students in a computing bootcamp at SNU Graduate School of Data Science.

Uploaded by

rmstn365
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

[Lecture 3] Strings and Control structures

The document covers fundamental programming concepts including string types, built-in operations on strings, and control structures such as Boolean types and if/else statements. It emphasizes the importance of handling various data types and provides examples of string manipulation, input/output operations, and decision-making in programming. The content is aimed at students in a computing bootcamp at SNU Graduate School of Data Science.

Uploaded by

rmstn365
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Review

• Function structure (header and body)

• Namespace and local variable

• What happens when you call a function

• Guidelines for writing a new function

Computing Bootcamp SNU Graduate School of Data Science 1


Computing Bootcamp

Strings

Lecture 3-1

Hyung-Sin Kim

SNU Graduate School of Data Science


Computing Bootcamp SNU Graduate School of Data Science 2
Programming for Big “Data”
• One of the main goals for this course is for you to handle various types of
data more easily

• Yes, you need to be familiar with various types of data and how to
represent and handle them

• We will see representative data structures provided by Python

• Let’s start from strings!

Computing Bootcamp SNU Graduate School of Data Science 3


String Type
• Recall that Python uses int and float types to represent number values
• Python defines another type, string (str), to represent text values
• Text is a sequence of characters (letters, digits, and symbols)

• Python recognizes that a value is string if it is surrounded by ‘’ or “”


• ‘Programming Foundations’
• “Programming Foundations”

• 19 vs. “19”

Computing Bootcamp SNU Graduate School of Data Science 4


Built-in Operations on Strings
• len(‘string’): Number of characters of the string
• len(‘Programming for Data Science ^0^/’)
• 33

• ‘string’ + ‘string’: Concatenation


• “I” + “ ” + “don’” + ‘t’ + ‘ like ’ + “COVID” + ‘-’ + ‘19’
• I don’t like COVID-19

• “string” * num: Repetition


• “(– –)(_ _)” * 5
• “(– –)(_ _)(– –)(_ _)(– –)(_ _)(– –)(_ _)(– –)(_ _)”

Computing Bootcamp SNU Graduate School of Data Science 5


Built-in Operations on Strings
• Type changes are possible
• int(‘1’) 1
• float(‘-234.2’) -234.2
• str(5) ‘5’

• Strings are values, so you can assign a string to a variable


• my_name = “Hyung-Sin Kim”
• len(my_name) 13
• my_name * 2 “Hyung-Sin KimHyung-Sin Kim”
• my_name + “ teaches this course” “Hyung-Sin Kim teaches this course”

Computing Bootcamp SNU Graduate School of Data Science 6


Special Characters in Strings
• I’m studying
• “I’m studying”
• I said “I’m studying”
• ???? Need another way
• “I said \“I\’m studying\””
• Escape sequence (sequence escaping from Python’s usual syntax rules)
• \’: Single quote
• \”: Double quote
• \\: Backslash
• \t: Tab
• \n: Newline
• \r: Carriage return

Computing Bootcamp SNU Graduate School of Data Science 7


Printing
• print(1+1) 2
• print(“I like this.”) I like this.
• print(1, 2, 3) 123

• radius = 3
• print(“The diameter of the circle is”, radius * 2, “m.”)
The diameter of the circle is 6 m.
• print(“Name\tNationality\nKim\tKorean\nCuller\tAmerican”)
• Name Nationality
• Kim Korean
• Culler American

Computing Bootcamp SNU Graduate School of Data Science 8


Getting Input from the Keyboard
• a = input()
• Computer waits for you to type something
• Whatever you type, Python represents the value as a string
• a = input()
• 10438482
• a
• ‘10438482’
• Input can get a string argument, which is used to prompt the user for input
• > name = input(“Please enter your name: ”)
• Please enter your name: Hyung-Sin Kim
• > name
• ‘Hyung-Sin Kim’

Computing Bootcamp SNU Graduate School of Data Science 9


Summary
• String values and operations

• Special characters

• Print and Input

Computing Bootcamp SNU Graduate School of Data Science 10


Computing Bootcamp

Control Structures
– Boolean Types
Lecture 3-2

Hyung-Sin Kim

SNU Graduate School of Data Science


Computing Bootcamp SNU Graduate School of Data Science 11
Introduction
• Making choices is a fundamental concept of programming

• We do this whenever we want our program to behave differently


depending on the data it’s working with
• Ex 1) Depending on whether a user types yes or no
• Ex 2) Depending on whether the number of students is less than 40 or not

• Today, you will learn


• Control flow statements which are for making choices
• These statements involve another Python type called Boolean that represents truth
and falsehood

Computing Bootcamp SNU Graduate School of Data Science 12


Boolean Type
• Type “bool” has only two values, True and False

• Boolean operators: not, and, or


• Boolean variables and operations
• >>> cold = True
• >>> windy = False
• >>> (not cold) and windy
• False
• >>> not (cold and windy)
• True

Computing Bootcamp SNU Graduate School of Data Science 13


Relational Operators
• A comparison using a relational operator results in a bool-type value
• >, <, >=, <=, ==, !=
• WARNING: == is for equality but = is for assignment
• Examples
• >>> 22 > 10 True
• >>> 30 > 40 False
• >>> 55 == 55 True
• >>> 56 != 56 False
• Useful function using relational operators
• def is_positive(x: float) -> bool:
• return x > 0

Computing Bootcamp SNU Graduate School of Data Science 14


More Complex Comparisons
• Combining comparisons:
• Precedence: arithmetic operators / relational operators / Boolean operators
• >>> True and (3 == 3) and (7 > 3+4) False
• PRACTICE: Use parentheses whenever you think your expression may not be
clear

• Short-circuit evaluation
• Python draws a conclusion fast if it is obvious, without evaluating further
• >>> (2 > 3) and (5 > 7) and (5 == 5)
• >>> (3+5 == 8) or (1 / 0)
• >>> (0 > 1) and duguwe384ihoslslsjlkjsdlfijoijeroijhpojfdslkmglkl_sls930kgk

Computing Bootcamp SNU Graduate School of Data Science 15


ASCII Code and Comparing Strings
• Character encoding standard
• Represented by integers
• >>> ord(“A”) 65

• Comparing strings (dictionary ordering)


• >>> “A” < “B” True
• >>> “A” < “a” True
• >>> “abc” < “abd” True
• >>> “abdaaa” < “abc” False
• >>> “abc” < “abcd” True

Computing Bootcamp SNU Graduate School of Data Science 16


A String inside Another String
• “in” operator (case sensitive)
• >>> “Sep” in “09 Sep 2020” True
• >>> “Jan” in “09 Sep 2020” False
• >>> “” in “abc” True
• >>> “” in “” True (the empty string is a substring of every string)

• An example
• >>> birthday = input(“Enter your birthday in the format DD MTH YYYY: ”)
• Enter your birthday in the format DD MTH YYYY: 01 Jan 2000
• >>> “Jan” in birthday True

Computing Bootcamp SNU Graduate School of Data Science 17


Summary
• Boolean type

• Relational operators and comparisons

• “in” operator

Computing Bootcamp SNU Graduate School of Data Science 18


Computing Bootcamp

Control Structures – If/Else

Lecture 3-2

Hyung-Sin Kim

SNU Graduate School of Data Science


Computing Bootcamp SNU Graduate School of Data Science 19
If Statement
• if statement lets you change how your program behaves based on a
condition
• if <<condition>>:
• <<block>>
• The block must be indented (similar to function body)
• The block is executed only when the condition is True
• More complex forms
• if <<condition1>>:
• <<block1>>
• elif <<condition2>>:
• <<block2>>
• else:
• <<block3>>

Computing Bootcamp SNU Graduate School of Data Science 20


If Statement
• Example (Let’s do it together)
• >>> time = input(“Enter the current time in the format HH:MM: ”)
• Enter the current time in the format HH:MM: 11:15
• >>> if time < “11:00”:
• … print(“Before Programming Foundations”)
• >>> elif time < “12:15”:
• … print(“During Programming Foundations”)
• >>> else:
• … print(“After Programming Foundations”)

Computing Bootcamp SNU Graduate School of Data Science 21


Nested If Statements
• if statements in another if statement
• if <<condition1>>:
• <<block1>>
• if <<condition1-1>>:
• <<block1-1>>
• elif <<condition1-2>>:
• <<block1-2>>
• else:
• <<block1-3>>
• else:
• <<block2>>

Computing Bootcamp SNU Graduate School of Data Science 22


Nested If Statements
• Example (assume that you have two variables age and bmi)
• young = (age < 45)
• slim = (bmi < 22.0)
• if young:
• if slim:
• risk = “low”
• else:
• risk = “medium”
• else:
• if slim:
• risk = “medium”
• else:
• risk = “high”

Computing Bootcamp SNU Graduate School of Data Science 23


Summary
• “if” and “else” statements

• Nested if statements

Computing Bootcamp SNU Graduate School of Data Science 24


Thanks!

Computing Bootcamp SNU Graduate School of Data Science 25

You might also like