Class Notes (1,100,000)
CA (620,000)
UTSC (30,000)
All (10)
Lecture 2

CSCA08 Lecture 2: CSC108 - Week 2 Notes


Department
Computer Science
Course Code
CSCA08H3
Professor
All
Lecture
2

This preview shows page 1. to view the full 5 pages of the document.
Alexander Magony
CSC108H1F 2014
Week 2
Type str
-string literal: a sequence of characters. Is the type str and is contained by single or
double quotes.
eg. ‘hello’ or “hello”
strings are values and can be stored in variables (eg. sunny_greeting = “What a
beautiful day!”).
Different symbols can be used in strings (@, *. -. etc). However, ‘You’re so cold’ will
return a SyntaxError since the internal single quote prematurely ends the string.
This can be fixed by changing the containing quotes to double quotes: “You’re so
cold”.
-Alternatively an escape character/sequence can be used. ‘You\’re so cold.’ will
return “You’re so cold.”
-Can add strings together. Cannot add a str to an int or a float (returns TypeError).
Even if the int or float is in quotations (because that makes it a str like ‘5’).
eg. ‘personal’ + ‘penguin’ returns ‘personalpenguin’. Spaces have to be included in
the quotes (eg. ‘personal ‘ + ‘penguin’ returns ‘personal penguin’).
You can also link variables that refer to strings. Example:
name = ‘Alex’
phrase = ‘ watch out’
punctuation = ‘!’
name + phrase + punctuation returns ‘Alex watch out!’
-Strings can be multiplied by ints (floats or str’s will return TypeErrors).
eg. ‘ha’ * 5 returns ‘hahahahaha’)
Order of operations still takes precedence.
-‘Bwa’ + ‘ha’ * 5 returns ‘Bwahahahahaha’
-Dividing strings, raising strings to exponents, and other inappropriate operations will
result in TypeErrors.
Input/Output and str Formatting
-print(argument): prints a sequence of arguments for user to read.
eg. print(“hello”) returns hello. There are no quotes as quotes only belong to
represent strings within Python’s internal structure, not for the user.
eg. print(3 + 7 - 3) returns 7.
eg. print(“hello”, “there”) returns hello there. Notice spaces are included when
printing multiple arguments.
1
You're Reading a Preview

Unlock to view full version

Only page 1 are available for preview. Some parts have been intentionally blurred.

Alexander Magony
CSC108H1F 2014
-Let’s create two similar functions where one prints and one returns.
def square_return(num):
return num ** 2
def square_print(num):
print(“The square of num is”, num ** 2)
-Then, if we execute: answer_return = square_return(4) nothing happens. But if we
then enter answer_return, 16 is returned.
-If we execute: answer_print = square_print(4) then The square of num is 16 is
returned. If we then execute answer_print, the value None is returned (visually, nothing
returns) since there is no return statement.
NoneType: the type of value that results when no return statement is used in a function
body. The actual value of this type is None which is used to represent nothing being
returned.
-So answer_return represents a numeric value and can be used in calculations (eg.
answer_return * 5 returns 80). However, answer_print * 5 will result in a TypeError since
answer_print refers to NoneType.
-When a return statement executes, it passes a value back to the caller and exits the function.
So if a function contains a print statement after a return statement, the print statement will
never be reached.
-input(prompt): a function that allows Python to get a string from the user. Returns the string
entered by the user (even if a number is typed, it is a string like ‘2’). The prompt is the
argument to input.
eg. input(“What is your name? “) returns What is your name? (where a cursor is flashing at
the end, waiting for the user to type something and press enter). If Jen is typed, ‘Jen’
returns, the single quotations indicating that it is type str.
The result can be stored. eg. location = input(“What is your location? “) returns What is
your location? followed by a blinking cursor waiting for the user. If Toronto is typed in that
same line, ‘Toronto’ will be the str value that is stored at the memory address contained in
the variable location.
-If name was the variable used to refer to ‘Jen’, we could enter print(name, “lives in”,
location) and print Jen lives in Toronto. Remember that spaces are automatically entered
when print uses several arguments.
-Triple quoted strings are able to span multiple lines. Example: if we enter
>>> print(‘’’How
are
you?’’’)
the printed statement is:
How
are
you?
-However, if we enter:
2
You're Reading a Preview

Unlock to view full version