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