Midterm review Flashcards
Zoom!!
Variables
a bucket to store information in
ex) camelCase or snake_case
Integers
int
3 or -1 or 0 or 2001
Real Numbers
float
3.14 or 0.094 or -12.0
Character Strings
str
“Hi” “ “ “a” “44”
Boolean
bool
True or False
What is this? 3 != 3
boolean bc it’s a comparison
What is this? “Exam, Day, Soon”
string
What is this? 5.9
float
What is this? 2
integer
What is this? 5.9 - 3
float
What is this? 9 // 1
integer ( just 9 divided by 1)
What is this? 5.9 x 3
float
Concatenation
- fancy name for string
- has “ “ or ‘ ‘
- concatenate by +
- like gluing strings
together (no spaces)
- like gluing strings
What is a method?
- function or command
- “called” with parenthesis
- to access method, use dot.operator
What are the two parts of a string?
it’s data and it’s methods
s = “panang Curry”
method = upper()
print(s.upper())
output?
PANANG CURRY
uppercase version of string
s = “panang Curry”
method = lower()
print(s.lower())
output?
panang curry
lowercase version of string
s = “panang Curry”
method = swapcase()
print(s.swapcase())
output?
PANANG cURRY
case of each letter is switched
s = “panang Curry”
method = capitalize()
print(s.capitalize())
output?
Panang curry
1st letter capitalized and the rest are lowercase
s = “panang Curry”
method = title()
print(s.title())
output?
Panang Curry
first letter of each word is caps and rest is lowercase
How do you display:
She said “You need escape characters”
Which are: \important\
print(“She said "You need escape characters"” \nWhich are: \important\”
Which escape character lets you use double quotes in a string?
"
Which escape character lets you move to a new line?
\n
Which escape character allows you to include backslash in output?
\
Which escape characters allow for indents in strings?
\t
User input
- store input IN VARIABLE
- ALWAYS returns a string
ex) num = input(“Enter a number: “) # num is string
input(“Enter a number: “) # useless
Branching
- CONDITION that evaluates whether a boolean is true or false
- mutually exclusive
- only ONE statement will run
- multiple ifs = each if statement can potentially run depending on statement
if CONDITION
- # do something
- only need if statement, doesn’t need elif or else
elif CONDITION
- # do something
- NEEDS if statement
else:
- # do something
- NEEDS if statement
= vs ==
= used for assigning var to new value
== used in your condition for comparison in if statements and while loops– evaluates to true false
ex) var = 3
print(3==3) TRUE
What’s the error in this branching example?
name = input(“What’s your name”)
if name == “Kyle” or “Beth”:
print(“Please come in”)
name = input(“What’s your name: “)
everything has an associated truth value
e.g.
non-empty string = True
non-zero number = True
non-empty list = True
so “Beth” evaluates to True always
Tracing if-statements
x = 10
if x > 3 and x % 2 == 0:
print(“Success”)
else:
print(“Fail”)
1) What will get printed?
2) What values of x will print “Success”?
1) print(“Success”)
2) x = 10
While loop
- Repeat code without writing repeated cote
- Great for when you r loop should break: “I wan to loop until the user inputs’q’”
- while userinput !=’q’
For Loop
- for each loop
- for char in
“abcdefghijklmno”
- for char in
- Range loop
- for i in range(start, stop,
step) - for num in range(1)
- for num in range(0,10)
- for num in range(0,10,1)
- for i in range(start, stop,
List and strings are SEQUENCES
- accessing individual elements
- indices start at 0
- string example
- name= “jenny”
- list example
- name= [“jenny”, “bob”,
“tina”]
List
- a collection of items
- create a new list using list() or [ ]
- are mutable
someList.append(value)
adds value to end of a list
someList.remove9vale)
Removes the first occurrence of value from the list
List slicing
- used for getting a subset of a list
- list slicing returns another list
- specify range of indices
- someList[start:end]
items = [“shirt”, “shoes
, “pants”, “hat”, “socks”, “tank top”]
print(items[2:5])
What gets printed?
*hint– first item of a list always starts with the number 0
hat socks
split( ) and join ()
- split turns a string into a list
- join turns a list into a string
charList = excelRow.split(“,”)
newString = “.”join(someList)
1 items = [“shirt