1/58
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Programming
The act of designing, implementing, and testing computer programs
Hardware
consists of the physical elements in a computer system.
ex: monitor, the mouse, external storage, and the keyboard.
what does the central processing unit (CPU) do?
performs program control and data processing
The CPU has what two components?
control unit
arithmetic logic unit
Control unit
directs the operation of the processor
communication and coordination between input/output devices.
reads and interprets instructions
determines the sequence for processing the data
provides timing and control signal
arithmetic logic unit
contains the circuitry to perform calculations and do comparisons
Primary storage
composed of memory chips: electronic circuits that can store data as long as it is provided electric power
Secondary storage
provides a slower, less expensive storage that is persistent
the data persists without electric power
Algorithm
describes a sequence of steps that is:
Unambiguous
No “assumptions” are required to execute the algorithm
The algorithm uses precise instructions
Executable
The algorithm can be carried out in practice
Terminating
The algorithm will eventually come to an end, or halt
variable
a named storage location in a computer program
example: total = 0
assignment statement
“=” is used to place a new value into a variable
what is an integer (int)?
A whole number (no fractional part)
ex: 7
what is a float?
A number with a fraction part
ex: 8.88
what is a string (str)?
A sequence of characters
ex: "Bob”
The data type is associated with the____, not the ____
value, variable
ex: cansPerPack = 6 #int
ex: canVolume = 12.0 #float
warning about variables and assigning different values at different places
If you mix stings with integer or floating point values the result is an error
naming variables
names must start with a letter or an underscore
continue with letters (upper or lower case), digits or the underscore
You cannot use other symbols (? or %...) and spaces are not permitted
A constant is a variable….
whose value should not be changed after it’s assigned an initial value
naming a constant
use this format: UPPER_CASE
Mixing numeric types
If you mix integer and floating-point values in an arithmetic expression, the result is a floating- point value.
ex: 7 + 4.0 # Yields the floating value 11.0
double stars ** are used to…
calculate an exponent
floor division using the // operator
The “//” operator computes the quotient and discards the fractional part
what is % used as
to find a remainder
what is a library?
collection of code, written and compiled by someone else, that is ready for you to use in your program
what is a standard library?
a library that is considered part of the language and must be included
with any Python system
Floating-point to integer conversion
You can use the function int() and float() to convert between integer and floating point values:
balance = total + tax # balance: float
dollars = int (balance) # dollars: integer
Text, Characters, and strings
Text consists of characters
Characters are letters, numbers, punctuation marks, spaces, ….
A string is a sequence of characters
String Length
The number of characters in a string
(For example, the length of "Harry" is 5).
You can compute the length of a string using Python’s len() function:
length = len("World!") # length is 6
What will be the values of the variables num1 and num2 after the given set of assignments?
num1 = 20
num2 = 10
num1 = num1 + num2 / 2
num2 = num1num1 = 25.0, num2 = 25.0
What must be done first before you can use a function from the standard library?
the function must be imported
what is wrong with this?
result = num1 // num2 / num3 num1 = 20 num2 = 10 num3 = 2 print(result)
A variable is used before it is assigned a value.
What is wrong with the following code snippet?
((num1 + num2) * num3 / 2 * (1 - num4)there is an extra parenthesis
Which statement correctly creates a new variable by combining the two string variables: firstName and lastName?
name = firstName + lastName
What is printed from the following code snippet:
message = "ho.."
print(message * 3)ho..ho..ho..
What is printed by the following code snippet?
num = int("45") * float("1.5")
print(num)67.5
What is the index value of the letter 'h' in the string below ?
message = "hello"0
Assignment vs. Equality Testing
Assignment: makes something true
floor = 13
Equality testing: checks if something is true
if floor == 13
Which statement finds the last letter of the string variable name?
last = name[len(name)]
last = len(name) - 1
last = len(name)
last = name[len(name) - 1]
last = name[len(name) - 1]
Which username would be accepted as valid?
username = input("Enter username: ")
if " " in username or not username.endswith("3"): print("Invalid")else: print("Valid")
A. truman 1839
B. truman18393
C. 1839Truman
D. Tru18man39
B. truman18393
What is printed by the following code snippet?
cost = 25.45378
print("%.2f" % cost)
A.) 25.45378
B.) %25.45
C.) 25.45
D.) nothing, there is an error
C.) 25.45
What is the data type of the value returned by the input function?
string
What is the output of the following code snippet if count contains 56?
if count % 2 == 0 :
print("Count is an even number")
else :
print("Count is an odd number")A.) Count is an even number
B.) Count is an odd number
C.) Nothing, there is a syntax error
D.) Nothing, the program runs but does not print any output
C.) Nothing, there is a syntax error
What value causes the following logical expression to 'short-circuit'?
if temp >= 32 and temp <= 100temp = 0
What string method can be used to determine if the string contained in the variable text only consists of numbers?
text.isalnum()
text.isalpha()
text.isdigit()
text.islower()
text.isdigit()
Using De Morgan's law, what is the equivalent to this statement?
if not (state == "PA" or state == "OH")if state != "PA" and state != "OH"
Boolean Variables
it is often called a flag because it can be either up (true) or down (false)
there are two operators: and, or
they are used to combine multiple conditions
Combined Conditions: and
Both sides of the and must be true for the result to be true
ex:
if temp > 0 and temp < 100 :
print("Liquid")
Combined Conditions: or
We use or if only one of two conditions need to be true
ex:
if temp <= 0 or temp >= 100:
print("Not liquid")
The not operator: not
If you need to invert a boolean variable or comparison, precede it with not
ex:
if not attending or grade < 60 :
print("Drop?")
The not operator: inequality !
sed for the not when checking for inequality rather than negation.
Example inequality:
The password that the user entered is not equal to the password on file.
if userPassword != filePassword :
De Morgan’s law
tells you how to negate and and or conditions
A while loop that is controlled by a ____
counter
ex:
counter = 1 # Initialize the counter
while counter <= 10 : # Check the counter
print(counter)
counter = counter + 1 # Update the loop variable
Infinite Loops
The loop body will execute until the test condition becomes False
sentinel value
denotes the end of a data set, but it is not part of the data
Common Loop Algorithms - Average Example
total = 0.0
count = 0
inputStr = input("Enter value: ")
while inputStr != "" :
value = float(inputStr)
total = total + value
count = count + 1
inputStr = input("Enter value: ")
if count > 0 :
average = total / count
else :
average = 0.0
Common Loop Algorithms - Sum Example
total = 0.0
inputStr = input("Enter value: ")
while inputStr != "" :
value = float(inputStr)
total = total + value
inputStr = input("Enter value: ")
Uses of a for loop:
The for loop can be used to iterate over the contents of any container.
A container is is an object (Like a string) that contains or stores a collection of elements
A string is a container that stores the collection of characters in the string
For Loop - while version
stateName = "Virginia"
i = 0
while i < len(stateName) :
letter = stateName[i]
print(letter)
i = i + 1
For Loop - for version
stateName = "Virginia"
for letter in stateName :
print(letter)