1/115
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
How do you read user input in python and store it in a variable?
use input() aka name = input(“enter your name”)
what issue can occur when numerical values are inputted by the user?
input() returns as a string, so numeric input must be converted for numeric type calculations
how do you convert numeric user input from a string to a number?
use int() for integers, and float() for decimals, aka age = int(input(“enter age: “))
what are the python symbols for the basic mathematical operators?
+ (addition), - (subtraction), * (multiplication), / (division), // (integer division), % (modulus), ** (exponentiation).
what is an operand
a value or vairiable which an operator performs an operation, (2+3) and 2, 3 are oprands
how do you store the result of a calculation in a variable?
use =
how do we type percentages in a calculation?
divide the percentage by 100
what are two division operators in python and what do they return?
/ returns with a float, // returns an integer (floor divison)
does integer division round up of down
down (towards negative infinity
how do you determine the remainder of a division?
use the modulus operator %, aka 7 % 3 returns 1
how do you perform exponentation
use **
what is the precedence of operators in python
pemdas
what is a magic number
a number used directly in code
what is a constant
a variable that the value is not meant to change
what is the naming convention for constants
typically all uppercase letter
what is a syntax error?
error caused by iccorect python code strucutre, missing parenthesis etc.
what is a logic error
an error code where code runs but produces the wrong result
what is an exception?
error detected during program execution
what is the alternate name for an exception
runtime error
what does name error mean
attempting to use a variable that has not been defined
what does type error mean
an operation or function that is applied to the wrong data type
what is data type conversion
changing value from one data type to another, ex string to an integer
what is implicit conversion
automatic type of conversion done by python in calculations
what are rules for implicit conversion
integer + integer = integer
float + float = float
integer + float = float
how do we perform explicit conversion?
use int() for float () to manually convert types when needed
how does int() work
converts to an integer rounding down
how does float() work
converts to a float
why do we perform explicit conversion:?
because intput() automatically returns a string
what is the definition of software
a collection of programs that perform tasks on a computer
what is a program
a set of instructions that tell the computer how to perform a task
what is a statement
indevidual commands in a program
what are the steps in a program development cycle
analyze the program
design the program
code (write) the program
test/debug the program
document the program
maintain/modify the program
what is the difference between syntax error and logic error
syntax - mistake in the code structure prevents the code from running
logic error - code runs but inccorect results
what is IPO
input, process, output
how to construct a program flowchart,
start/end - oval
input - parralellogram
process- rectangle
output- paralellogram
can numeric types hold negative numbers,
yes both int and float can be negative
how must True and False be written in bool, with or without quotes?
capitalized without quotes
how can you identify the data type of a value,
use the type() function
what is a function
a reusable block of code that performs a task
what is an argument
values passed on to a function for processing
what is a variable, and how is it related to ram
variable is a named location in memory that stores a value
How do you write an assignment statement? What is the assignment operator?
Use =; e.g., x = 5.
What happens with variable reassignment?
The variable’s old value is replaced with the new value in memory.
Can a variable be reassigned to a different value or data type?
yes, it can hold a new value and can change data types.
Can a variable be reassigned to another variable?
Yes, e.g., y = x.
What are the rules for naming variables? What convention do we use?
Cannot start with a number, contain spaces, or reserved keywords
Can use letters, numbers, and underscores
Convention: snake_case, e.g., student_name
What is a string literal?
Text written in quotes, e.g., "Hello".
What is a numeric literal? Can special symbols be included?
A numeric value written directly in code, e.g., 100. Special symbols like $, %, , cannot be included in numeric literals.
What happens if commas are added to a numeric literal?
it causes a SyntaxError; Python does not accept commas in numbers.
Can a number be a string literal? Implication in calculations?
Yes, e.g., "5" is a string. Must convert to int() or float() to use in calculations.
How do you display multiple arguments in print()?
Separate them with commas, e.g., print("Age:", age).
What character is automatically inserted between arguments?
A space.
How do you add a comment? Purpose?``
Use # for single-line comments. Purpose: explain code to humans; ignored by Python.
How should comments be aligned?
Aligned with the code they describe (may be indented to match code block).
Difference between Shell Window and Editor Window? Purpose of both?
Shell Window: Interactive; shows immediate results.
Editor Window: Write and save full programs.
To see a variable’s value in the Shell, do you need print()?
Not always; typing the variable shows its value. print() is needed in scripts.
Keyboard shortcuts in IDLE (Windows)?Run program: F5
Save: Ctrl + S
Copy/Paste: Ctrl + C / Ctrl + V
Undo: Ctrl + Z
What is an escape character?
A backslash (\) followed by a character that has a special meaning in a string.
What is the escape character for a new line? Where is it typed?
\n – typed inside a string, e.g., "Hello\nWorld".
What is the escape character for a tab? Where is it typed?
\t – typed inside a string, e.g., "Name:\tJohn".
What is a positional argument? Does order matter?
An argument assigned by position; yes, order matters.
What is a keyword argument? Does order matter?
An argument specified with its parameter name; order does not matter.
What is the purpose of the sep keyword argument in print()? What is the default value?
sets the separator between arguments. Default is a space " ".
What is the purpose of the end keyword argument in print()? What is the default value?
Sets what is printed at the end. Default is a newline "\n".`
How do you use both sep and end in one print()?
print("A", "B", sep="-", end="!") → A-B!.
What is string repetition? What symbol is used?
Repeating a string using *. Example: "Hi" * 3 → HiHiHi.
`
What is string concatenation? What symbol is used?
Joining strings with +.
How do you store a concatenated string in a variable?
full = "Hello" + " " + "World".
How do you embed concatenation in print()?
print("Hello" + " " + "World").
How do you break long statements into multiple lines?
Use the backslash \ at the end of a line.
How do you spread a function call with many arguments across lines?
Place each argument on a new line within parentheses, e.g.:
print("one", "two", "three",
"four", "five") How do you construct an f-string?
Prefix with f and use {} for variables, e.g., f"Hello {name}".
How do you format strings, integers, floats, and percents with f-strings?
String: f"{name}"
Integer: f"{age:d}"
Float: f"{price:.2f}"
Percent: f"{rate:.0%}"
How do you write multiple placeholders in an f-string?
f"{name} is {age} years old".
What is a format specifier?
Extra instructions inside {} that control alignment, width, commas, precision, and type.
How do you align columns with f-strings?
Use width and alignment, e.g., f"{name:<10}{score:>5}".
How do you nest an f-string in print()?
print(f"{item}: {price:.2f}").
How do you save an f-string to a variable?
message = f"Hello {name}".
How do you use f-strings for tabular data?
Format rows with consistent width/alignment, e.g.:
print(f"{'Item':<10}{'Price':>8}")
print(f"{'Apple':<10}{1.25:>8.2f}") What is a control structure?
A block of code that determines the flow of program execution.
Difference between sequence, decision, and repetition structures?
Sequence: Executes statements in order.
Decision: Executes statements based on a condition (IF).
Repetition: Repeats statements while a condition is true (loops).
How do you construct a decision structure in a flowchart? Which symbol is used?
Use a diamond shape for decisions; flow branches based on Yes/No outcomes.
How do you write a basic IF statement?
if condition:
# block of code
What is an IF clause? What special character ends it?
The line with if and the Boolean expression; ends with a colon :.
What is a block?
The indented lines of code that execute if the condition is True.
How can we recognize the end of an IF structure?
The block ends when indentation returns to the previous level.
What is a Boolean expression? Synonyms?
An expression that evaluates to True or False; also called conditional test or logical test.
Which part of an IF clause is the Boolean expression?
The condition following if.
What are comparison (relational) operators?
== (equal), != (not equal), <, >, <=, >=.
How do you construct an IF-ELSE statement?
if condition:
# block1
else:
# block2 Why does a NameError occur in an IF-ELSE?
Because a variable name is misspelled or undefined.
How do you construct an IF-ELSE-IF structure?
Multiple IF-ELSE statements chained; less preferred.
Why does a NameError occur in an IF-ELSE?
Because a variable name is misspelled or undefined.
How do you construct an IF-ELSE-IF structure?
Multiple IF-ELSE statements chained; less preferred.
How do you construct an IF-ELIF-ELSE structure? Why is it preferred?
if condition1:
# block1
elif condition2:
# block2
else:
# block3
How do you test a range of values in an IF statement?
Use comparison operators; e.g., if 50 <= score <= 100:
Rule of thumb for number of IFs?
Usually one IF per category/value range.
How to code a series left to right vs right to left?
Left to right: if 50 <= score <= 100:; Right to left: if score >= 50 and score <= 100:
Why avoid using and in a series unnecessarily?
Can create logic errors and redundancy.
Why avoid independent IFs in a series?
They may all execute even if one condition is met; use IF-ELIF-ELSE instead.