Podcast
Questions and Answers
Which of the following is the most accurate description of a 'constant' in programming?
Which of the following is the most accurate description of a 'constant' in programming?
- A value that changes every time the program is executed.
- A reserved keyword used for mathematical operations.
- A named memory location that stores data.
- A fixed value, such as a number, letter, or string, whose value does not change during program execution. (correct)
Why are certain words like if
, else
, and while
designated as 'reserved words' in Python?
Why are certain words like if
, else
, and while
designated as 'reserved words' in Python?
- To prevent them from being used as variable names, as they have predefined meanings in the language's syntax. (correct)
- To allow programmers to use them freely as variable names.
- To serve as comments within the code.
- To indicate the end of a program.
Which of the following statements best describes a 'variable' in programming?
Which of the following statements best describes a 'variable' in programming?
- A fixed value that cannot be altered during program execution.
- A keyword used to define functions.
- A data type that represents only numeric values.
- A named storage location in memory that can hold a value, which can be changed during program execution. (correct)
Which of the following demonstrates a valid variable name in Python?
Which of the following demonstrates a valid variable name in Python?
Given the Python code:
x = 35.0
y = 12.50
z = x * y
print(z)
What will be the output of this code?
Given the Python code:
x = 35.0
y = 12.50
z = x * y
print(z)
What will be the output of this code?
What is the primary function of an 'assignment statement' in Python?
What is the primary function of an 'assignment statement' in Python?
Consider the following Python assignment statement: y = x + 2
. What does this statement do?
Consider the following Python assignment statement: y = x + 2
. What does this statement do?
Given the following expression in Python:
x = 3.9 * x * (1 - x)
If x
initially holds the value 0.6
, what is the new value of x
after this statement is executed?
Given the following expression in Python:
x = 3.9 * x * (1 - x)
If x
initially holds the value 0.6
, what is the new value of x
after this statement is executed?
According to operator precedence rules, which operation is performed first in the following Python expression?
x = 1 + 2 * 3 - 4 / 5 ** 6
According to operator precedence rules, which operation is performed first in the following Python expression?
x = 1 + 2 * 3 - 4 / 5 ** 6
Consider the expression: x = 1 + 2 ** 3 / 4 * 5
. According to Python's order of operations, what is the value of x
?
Consider the expression: x = 1 + 2 ** 3 / 4 * 5
. According to Python's order of operations, what is the value of x
?
In Python, what does it mean when we say a variable has a specific 'type'?
In Python, what does it mean when we say a variable has a specific 'type'?
Which of the following is an example of implicit type conversion in Python?
Which of the following is an example of implicit type conversion in Python?
What will be the result of the code print(10 / 2)
in Python 3?
What will be the result of the code print(10 / 2)
in Python 3?
What happens if you try to convert a string containing non-numeric characters (e.g., 'hello') to an integer using the int()
function in Python?
What happens if you try to convert a string containing non-numeric characters (e.g., 'hello') to an integer using the int()
function in Python?
What is the primary purpose of the input()
function in Python?
What is the primary purpose of the input()
function in Python?
If you prompt the user for a number using the input()
function, what data type is returned?
If you prompt the user for a number using the input()
function, what data type is returned?
In Python, what is the purpose of comments?
In Python, what is the purpose of comments?
Which symbol is used to denote a comment in Python?
Which symbol is used to denote a comment in Python?
Which of the following code snippets correctly prompts the user for their name and then prints a greeting?
Which of the following code snippets correctly prompts the user for their name and then prints a greeting?
Given the code:
inp = input('Europe floor?')
usf = int(inp) + 1
print('US floor', usf)
If the user enters 0
, what will be printed?
Given the code:
inp = input('Europe floor?')
usf = int(inp) + 1
print('US floor', usf)
If the user enters 0
, what will be printed?
What is the missing code?
a = 5
b = 6
___
a = b
b = c
What is the missing code?
a = 5
b = 6
___
a = b
b = c
What will happen when the following code is executed in Python?
x = '123'
y = x + 1
print(y)
What will happen when the following code is executed in Python?
x = '123'
y = x + 1
print(y)
What will be the output of the following Python code?
xx = 23
kk = xx % 5
print(kk)
What will be the output of the following Python code?
xx = 23
kk = xx % 5
print(kk)
Given the code:
yy = 440 * 12
zz = yy / 1000
print(zz)
What is the final output?
Given the code:
yy = 440 * 12
zz = yy / 1000
print(zz)
What is the final output?
Given the python code:
print(float(99) + 100)
What is the result?
Given the python code:
print(float(99) + 100)
What is the result?
Given the code, what is the result?
i = 42
print(type(i))
Given the code, what is the result?
i = 42
print(type(i))
Given the code:
i = 42
f = float(i)
print(f)
What is printed to the console?
Given the code:
i = 42
f = float(i)
print(f)
What is printed to the console?
What is the printed to the console after running the code:
print(10 // 2)
?
What is the printed to the console after running the code:
print(10 // 2)
?
What is the printed to the console after running the code:
sval = '123'
print(sval + 1)
?
What is the printed to the console after running the code:
sval = '123'
print(sval + 1)
?
Which python code will prompt somebody to enter their name?
Which python code will prompt somebody to enter their name?
If the user enters 0
when running the code
inp= input('Europe floor?')
usf = int(inp) + 1
print('US floor', usf)
what will be printed?
If the user enters 0
when running the code
inp= input('Europe floor?')
usf = int(inp) + 1
print('US floor', usf)
what will be printed?
Which line of code contains a comment?
Which line of code contains a comment?
If you wanted to know the amount of characters somebody typed into the console as their name, what code would you implement? Assume they already answered the input.
If you wanted to know the amount of characters somebody typed into the console as their name, what code would you implement? Assume they already answered the input.
If you wanted to assign a
to equal b
, and b
to equal a
, what code could you use?
If you wanted to assign a
to equal b
, and b
to equal a
, what code could you use?
Flashcards
Constants
Constants
Fixed values like numbers, letters, and strings.
Reserved Words
Reserved Words
Words that have predefined meanings in Python and cannot be used as variable names.
Variable
Variable
A named memory location where data can be stored and retrieved.
Python Variable Naming Rules
Python Variable Naming Rules
Signup and view all the flashcards
Statements
Statements
Signup and view all the flashcards
Assignment Statement
Assignment Statement
Signup and view all the flashcards
Expression
Expression
Signup and view all the flashcards
Operator Precedence
Operator Precedence
Signup and view all the flashcards
Type
Type
Signup and view all the flashcards
Integers
Integers
Signup and view all the flashcards
Floating Point Numbers
Floating Point Numbers
Signup and view all the flashcards
Type Conversion
Type Conversion
Signup and view all the flashcards
Integer Division
Integer Division
Signup and view all the flashcards
User Input
User Input
Signup and view all the flashcards
Comments
Comments
Signup and view all the flashcards
Study Notes
Constants
- Fixed values such as numbers, letters and strings are called constants because their value does not change
- Numeric constants are as expected
- String constants use either single quotes (') or double quotes (")
Reserved Words
- Reserved words cannot be used as variable names or identifiers
- Examples: False, None, True, and, as, assert, break, class, etc.
Variables
- A variable is a named memory location where a programmer can store data
- The data can be retrieved later by using the variable's name
- Programmers can choose the name of the variables
- The contents of a variable can be changed in a later statement
Python Variable Name Rules
- Variable names must start with a letter or underscore
- Variable names must consist of letters, numbers, and underscores
- Variable names are case-sensitive
- Reserved words cannot be used as variable names
Sentences or Lines
- Sentences or lines can be classified as assignment, assignment with expression or print statements
x = 2
is an assignment statementy = x + 2
is an assignment with expressionprint(y)
is a print statement- The components of a line or sentence are variables, operators, constants and functions
Assignment Statements
- Values are assigned to variables, using the = assignment statement
- An assignment statement consists of an expression on the right-hand side, and a variable to store the result
Expressions
- Lack of mathematical symbols on computer keyboards requires "computer-speak" to express classic math operations
Numeric Expressions
+
is used for addition-
is used for subtraction*
is used for multiplication/
is used for division**
is used for power%
is used for remainder
Order of Evaluation
- Python must know which operation to perform first when stringing operators together
- This is known as "operator precedence"
Operator Precedence Rules
- Highest precedence rule to lowest precedence rule:
- Parentheses are always respected
- Exponentiation (raise to a power)
- Multiplication, Division, and Remainder
- Addition and Subtraction
- Left to right
Type
- Variables, literals, and constants have a "type" in Python
- Python recognizes the difference between an integer number and a string
- The plus sign (+) indicates "addition" if something is a number, and "concatenate" if the thing is a string
String Concatenation
- String concatenation is putting strings together
Type Matters
- Python automatically recognizes the variable type
- Certain operations are prohibited
- It isn't possible to add a number to a string
type()
determines the variable type
Types of Numbers
- Numbers can be either int or float
- Integers are whole numbers, and have the type
int
- Floating point numbers have decimal parts, and have the type
float
Type Conversions
- An integer is implicitly converted to a float, if an expression contains both an integer and a floating point number
- The built-in functions
int()
andfloat()
can control this conversion
Integer & Float Division
- Integer division result uses the
//
operator - Floating point result uses the
/
operator - There was a different behavior in Python 2.x
String Conversion
- Use
int()
andfloat()
to convert between strings and integers - An error occurs if the string does not contain numeric characters
User Input
- Python pauses and reads user data entered using the
input()
function input()
returns a string
Converting User Input
int()
is used to convert user input from a string to a number
Comments in Python
- Use
#
to comment out lines of code, as Python ignores anything after it - Comments are used to describe code, document the author, or turn off a line of code
Coding Exercise 1: Band Generator Game
- Create a greeting for the program
- Ask the user for the city that they grew up in
- Ask the user for the name of a pet
- Combine the names of the user's city and pet to show them their band name
- Ensure the input cursor shows on a new line
- Use escape sequences
Coding Exercise 2: Correct Errors
- Correct the errors for display to match these lines
- Day 1 - String Manipulation
- String Concatenation is done with the "+" sign.
- e.g., print("Hello " + "World")
- New lines can be created with a backslash and n.
Coding Exercise 3: Input Function and len Function
- Google to find a function that calculates the length of a string
- Write a program that prints the number of characters of a user's name
Coding Exercise 4: Swapping Values
- Write a program that switches the values stored in the variables
a
andb
- e.g. swap variable
a = 5
andb = 6
, toa = 6
andb = 5
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.