DV Module 2 Variables, Comments & Input operators Flashcards
Things to avoid when making variable names
avoid using reserved Keywords and Functions as they are PREDEFINED
avoid caps in the middle of the name
avoid variable name being so long that it would lead to errors in typing it
avoid anything other then _ as they will not work with naming variables
What is known as the assignment operator?
=
can one assign the same variable with the new value
Code: var = 100 var = 200 + 300 print(var) Output ?
Seeing a record like that, a mathematician would probably protest - no value may be equal to itself plus one. This is a contradiction. But Python treats the sign = not as equal to, but as assign a value.
output: 500
How would a shortcut operator look with this examples
- i = i + 2 * j
- var = var / 2
- rem = rem % 10
- j = j - (i + var + rem)
- x = x ** 2
- i += 2 * j
- var /= 2
- rem %= 10
- j -= (i + var + rem)
- x **= 2
Exercise
What is the output of the following snippet?
var = 2
var = 3
print(var)
3
Exercise
Which of the following variable names are wrong in Python?
my_var
m
101
averylongvariablename
m101
m 101
Del
del
101 # incorrect (starts with a digit)
m 101 # incorrect (contains a space)
Del # incorrect (is a keyword)
Exercise
What is the output of the following snippet?
a = ‘1’
b = “1”
print(a + b)
11
Exercise
What is the output of the following snippet?
a = 6
b = 3
a /= 2 * b
print(a)
1.0
2 * b = 6
a = 6 → 6 / 6 = 1.0
How do you make a comment Python
use a # at the start of a line
The _____ function is able to read data entered by the user and to return the same data to the running program.
input ()
Two different type casting for function input()
int(input()
float(input()
What is another use for + $ * other then adding or multiplying operators?
adding and multiplying strings together.
The meaning of the positional parameter is determined by its:
Position
Appearance
Name
Position