Module 2 - Variables, expressions and statements Flashcards
T or F: Programs must be designed before they are written
True
You must write down the steps (pseudo code, flowchart)
Algorithm
Set of well-defined logical steps that must be taken to perform a task
What do the following mean in a flowchart?
Oval, rectangle, parallelogram
Oval = "terminal" = start or end Rectangle = process Parallelogram = input/output
What is the input function in Python? Give an example.
variable = input(prompt)
This statement assigns the input to a variable, the prompt is what the user sees and what prompts her to input data
For example,
age= input(“Enter your age”)
This assigns the user’s input of their age to the variable named “age”
Input function returns what data type? How do you change that?
String Add int() before input: variable=int(input("enter your age"))
What is a function?
A piece of code that performs an operation
A function is a set of statements that take inputs, do some specific computation and produces output. The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function
T or F - Python is case sensitive
True - it is cAsE sEnSitiVE!
Variable
A location in RAM
Values used in a program are stored in and retrieved from the memory unit. In High-level languages, symbolic names are used in place of actual memory addresses. These symbolic names used in this manner are called Variables
What are the three main data types?
Integer = int() = whole numbers Float = float() = numbers with decimals String = str() = letters
Can a variable be enclosed in quotes? For example, “salary” = 45
No!
“Salary” is a string
Variable naming rules
- cannot be a python keyword
- first character must be a letter or an underscore
- after first letter, can only use letters, numbers and underscores
- case sensitive
What is an expression? Give some examples
Any combination of variables, operations, and values that yields a result
(Also: anything that requires simplification)
Examples:
input(“type your name”)
int(“45”)
2+2
What is a string literal?
Anything enclosed in quotes
Examples: “hello”, “42” “23.3”
If you perform a math operation to the following data type operands, what is the resulting data type?
int, int
float, float
int, float
intint = int
floatfloat=float
int*float=float (int is temporarily converted to float for this expression)
What happens when you convert float to int data type? For example, 3.9999 to int?
Decimal is chopped off, it is NOT rounded
3.9999 becomes 3
How do you display multiple items with a single print function? How are they displayed?
Separate arguments with a comma: print(a,b)
Arguments are displayed in the order they are passed to the function, results are separated by a space
How do you break long statements into multiple lines?
use \ character
For example,
print(“yas”,\
“why not”)
What is the end of a print function? How do you change that?
The end of a print function is the newline character (the backslash which tells the program to start a new line after the print function)
There’s a special argument called end=’delimiter’ that will place the delimiter you specify instead of the newline character
What is the item separator when items are displayed in a print function? How do you change that? For example, change to new line
Space
Special argument sep=’delimiter’
print(“yes”,”why not”,sep=’\n’)
What is \n? What is \t?
Special characters in a string literal, they are commands embedded in a string
Represents new line and horizontal tab
How do you format display of numbers on screen?
Use format function, which has two arguments: numeric value to be formatted and format specifier
Returns string containing formatted number
What does the format specifier typically include?
Precision and data type
can indicate scientific notation, comma separators, and the minimum field width used to display the value
What do the + and * operators do on strings?
For example, “apples”+”oranges”
“Spam”*3
Concatenate and multiply, respectively:
applesoranges
SpamSpamSpam
What are the three types of errors when debugging?
- Syntax - structure and rules, Python can’t understand what you are saying
- Runtime - aka exceptions, occur after the program starts running, Python understands what you’re saying but runs into an error when executing
- Semantic errors - program runs without error messages but doesn’t do what you want
Assign the average of the values in the variables a, b, and c to a variable avg. Assume that the variables a, b, and c have already been assigned a value, but do not assume that the values are all floating-point. The average should be a floating-point value.
avg=(a+b+c)/3
/ will always result in a float
typically, you would need to convert one of the variables (a,b,or c) to float to have the result as a float, but not this time b/c because / always results in a float
Write a print statement to show “Hello” and “ World” separated by: 1. a new line
print(“Hello \nWorld”)