Module 2 - Variables, expressions and statements Flashcards

1
Q

T or F: Programs must be designed before they are written

A

True

You must write down the steps (pseudo code, flowchart)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Algorithm

A

Set of well-defined logical steps that must be taken to perform a task

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do the following mean in a flowchart?

Oval, rectangle, parallelogram

A
Oval = "terminal" = start or end
Rectangle = process 
Parallelogram = input/output
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the input function in Python? Give an example.

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Input function returns what data type? How do you change that?

A
String
Add int() before input: 
variable=int(input("enter your age"))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a function?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

T or F - Python is case sensitive

A

True - it is cAsE sEnSitiVE!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Variable

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the three main data types?

A
Integer = int() = whole numbers
Float = float() = numbers with decimals 
String = str() = letters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can a variable be enclosed in quotes? For example, “salary” = 45

A

No!

“Salary” is a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Variable naming rules

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an expression? Give some examples

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a string literal?

A

Anything enclosed in quotes

Examples: “hello”, “42” “23.3”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If you perform a math operation to the following data type operands, what is the resulting data type?
int, int
float, float
int, float

A

intint = int
float
float=float
int*float=float (int is temporarily converted to float for this expression)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens when you convert float to int data type? For example, 3.9999 to int?

A

Decimal is chopped off, it is NOT rounded

3.9999 becomes 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you display multiple items with a single print function? How are they displayed?

A

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

17
Q

How do you break long statements into multiple lines?

A

use \ character

For example,
print(“yas”,\
“why not”)

18
Q

What is the end of a print function? How do you change that?

A

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

19
Q

What is the item separator when items are displayed in a print function? How do you change that? For example, change to new line

A

Space
Special argument sep=’delimiter’
print(“yes”,”why not”,sep=’\n’)

20
Q

What is \n? What is \t?

A

Special characters in a string literal, they are commands embedded in a string
Represents new line and horizontal tab

21
Q

How do you format display of numbers on screen?

A

Use format function, which has two arguments: numeric value to be formatted and format specifier
Returns string containing formatted number

22
Q

What does the format specifier typically include?

A

Precision and data type

can indicate scientific notation, comma separators, and the minimum field width used to display the value

23
Q

What do the + and * operators do on strings?
For example, “apples”+”oranges”
“Spam”*3

A

Concatenate and multiply, respectively:
applesoranges
SpamSpamSpam

24
Q

What are the three types of errors when debugging?

A
  1. Syntax - structure and rules, Python can’t understand what you are saying
  2. Runtime - aka exceptions, occur after the program starts running, Python understands what you’re saying but runs into an error when executing
  3. Semantic errors - program runs without error messages but doesn’t do what you want
25
Q

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.

A

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

26
Q

Write a print statement to show “Hello” and “ World” separated by: 1. a new line

A

print(“Hello \nWorld”)