Control Structures Flashcards
Boolean
Binary variable, True or False
==
Creates boolean variable
ex: print(2==3) –> False
!=
Not equal operator
ex: print(1 != 1) –> False
> ,<
Determine if one float/integer is greater than or less than the other
ex: print(7 > 5) –>
True
print(10 < 10) –>
False
> =,<=
Determine if one float/integer is greater or equal to than or less than or equal to the other
ex: print(7 >= 5) –>
True
print(10 <= 10) –>
True
Lexicographically
How strings are compared, a=1 e=5, etc.
ex: print(“Annie” > “Andy”) –> True
if
Runs code if a certain condition holds True. Statement within are indented
ex: if 10 > 5:
print(“10 greater than 5”)
Can if statements be nested?
True
ex: num = 12
if num > 5:
print(“Bigger than 5”)
if num <=47:
print(“Between 5 and 47”) –>
Bigger than 5
Between 5 and 47
else
Runs code when the if statement is False
ex:
x = 4
if x == 5:
print(“Yes”)
else:
print(“No”)
elif
else if, used to chain else if statements
ex:
if num == 1:
print(“One”)
elif num == 2:
print(“Two”)
else:
print(“Something else”)
and
Evaluates True only if both statements are true
ex: print(1 == 1 and 2 == 2) –> True
print(1 == 1 and 2 == 3) –> False
or
Evaluates True if one or both statements are true, only evaluated False if both statements are false
ex:print(1 == 1 or 2 == 2) –> True
print(1 == 1 or 2 == 3) –> True
Not
Inverts argument
ex: print(not 1==1) –> False
== comes before and/or in order of operations
True
[]
Creates list
ex: words = [“Hello” , “World” , “!”]
What number is used to index the first number in a list?
0
ex: print(words[0])
All items need to be the same type in a list
False
ex: things = [“string”, 0, [1, 2, number], 4.56]
#note how list contains strings, integers, floats, lists, and variables!
Strings can be indexed like lists
True
ex: str = “Hello world!”
print(str[6]) –>
w
#spaces, ‘ ‘, count as a list item
You cannot change specific items in lists
False
nums = [7, 7, 7, 7, 7]
nums[2] = 5
print(nums) –>
[7, 7, 5, 7, 7]
in
Operator to determine if an item appears in a list
ex: print(“spam” in words) –>
True
‘not’ in ‘in’ operator
To check if an item is not in a list
ex:
print(not 4 in nums)
print(4 not in nums)
#note multiple ways
.append
Method that adds an item to the end of an existing list
ex:
nums = [1, 2, 3]
nums.append(4)
print(nums) –>
[1, 2, 3, 4]
len()
Function that give number of items in list
ex:
nums = [1, 3, 5, 2, 4]
print(len(nums)) –>
5
#starts at 1, not 0
.insert
Method that allows you to insert a new item anywhere in a list
ex:
words = [“Python”, “fun”]
words.insert(1, “is”)
print(words)