Data types and variables Flashcards
Define variable and identifier
Variable: Location in memory contains one or more data value
Identifier: Unique name given to variable, procedure, function, class or object
How does assignment happen
When the ‘=’ sign is used as a command
What are the 4 main datatypes and define them
Integer: Positive and negative whole numbers, includes zero
Float: Number with a decimal point
Boolean: True or False
string: Text
What are the 7 operators and define them
Addition (+) : Normal adding
Subtraction (-) : Normal subtracting
Multiplication (*): Normal multiplying
Division (/): Normal dividing
Indices (**): Powers
DIV (//): Integer division/quotient rounds down to nearest integer
MOD (%): Modulus also known as the remainder
Augmented assignment operators
+= —> same as x = x + 3
-= —> same as x = x - 1
*= —> same as x = x * 10
/= —> same as x = x/ 5
//= —> same as x = // 5
%= —> same as x = x % 7
Same as normal operators but uses less memory
Name different string manipulation
- variable.upper() - changes variable to upper case
- variable.lower() - changes variable to lower case
- variable.swapcase() - switches case of all characters
- variable.capitalize() - Capitalises first character in string
- variable.title() - Capitalises first character after every space
- variable.strip(chars) - Removes ‘chars’ from variable. If left blank removes leading white space
- variable.replace(old,new) - Replaces all old characters with new ones
- len(variable) - Returns number of characters
- variable[index] - Only character in index position is printed
- variable[start:end] - Only character in that specific range is printed. Includes the start does not include the end