Python 3 Flashcards
What are the four rules to naming variables?
- Variables are case sensitive.
- Variable names must be one word.
- Variable names may be letters, numbers, or underscore.
- Variable names cannot begin with a number.
Variables should begin with a ________ word, and multiple words should be separated with _________s.
lowercase , underscore
How do you assign an integer to a variable?
variable = integer x = 100
How do you assign a string to a variable?
variable = 'string' x = 'wes'
How do you print a variable?
print(variable)
print(x)
How do you assign multiple values to the same value?
variable1 = variable2 = variable3 = integer x = y = z = 0 x = y = z = 'wes'
How do you assign values to several different variables within the same line?
variable1, variable2, variable3 = value1, value2, value3
x, y, z = 1, ‘two’, 3
What is a global variable?
A global variable exists outside a function.
What is a function?
A function is a block of instructions that performs an action and, once defined, can be reused. Functions make code more modular, allowing you to use the same code over and over again.
What is a local variable?
A local variable exists within a function.
What is the python function to return an absolute value?
abs ( )
What are the five python data types?
- Numbers
- String
- List
- Tuple
- Dictionary
What are the three python numerical types?
- Integer
- Float
- Complex
What is the code to print the first letter in a string?
print(stringname[0])
What is a list?
A list contains items separated by columns and enclosed with brackets.
What is the code to create a list?
list = [1,2,4,’four’,5]
What is the code to print the 2 through 4th items in a list?If
print(list[1:4])
What is a tuple?
A tuple is a list of numbers separated by comma’s and enclosed in parenthesis.
What is the code to create a tuple?
tuple = (1,2,3, ‘four,5)
What is the major difference between a tuple and a string?
Tuple items cannot be changed, they are read only.
What are the 7 arithmetic operators supported by python?
- addition +
- Multiplication *
- Subtraction -
- Division /
- modulus %
- Exponent **
- Floor //
What is floor division?
The division of operands where the result is the quotient with any numbers after the decimal dropped. If one of the operands is negative then the result will be negative.
What are the 6 python comparison operators?
- If the value of two operands is equal then the condition becomes true. ==
- If the value of two operands are not equal, then the condition becomes true. !=
- If the value of the left operand is greater than the right operand then the condition becomes true. >
- If the value of the right operand is great than the left operand then the condition becomes true. <
- If the value of the left operand is greater than or equal to the right then the condition becomes true. >=
- If the value of the right operand is greater then or equal to the left operand then the condition becomes true. <=
What are the three python decision making statements?
- If statement
- If…else statement
- Nested if statement.
What is the code for a single if statement?
if variable == 1:
print(‘True’)
What is the code for a if… else statement?
if variable == 1:
print(‘True’)
Else:
print(‘False’)