week 2 variables Flashcards
variable
named storage spot (‘box’) that holds value assigned by three properties
three properties of a variable
name/identity, type, and value
int
single whole number, integer
real values/ floating
decimal (needs bigger ‘box’)
string
letter(s) “ “
boolean
true or false
type function
type()
determines type of variable, can be verified with print
i.e print(type(variable))
how many numbers can a variable store?
one, it can be altered through reassignment
defining strings
first = “Ada”
last = “Lovelace”
concatenating strings
full = first + last
full = “AdaLovelace”
-> no space
how do you add numbers to a string
convert num to string with str() function
len(char)
number of characters
user.lower()
lowercases all alphabets
indexing username[0]
returns first character, starts at 0
immutable
cant be changed, strings can’t be altered through indexing
username[7] = “i”
ERROR.
string slicing
lets you select sections of a string
my_str[0:9]
returns first 10 characters
my_str[10:-5]
does 5 indices after 10, including 10
so basically 10 11 12 13 14 15
my_str[8:]
everything after 8
my_str[:8]
everything before 8
my_str[:-1]
everything but last character
find(x)
returns the index of the first occurrence of string x
**
power sign
//
integer division, number without remainder
%
modulus, remainder
26 // 5
5
26 % 5
1
module
library in python that has list of functions
format()
used to format output specified in string before
print(“Total Cost: ${:.2f}”.format(cost))
formats the cost by adding a dollar sign and only including two numbers after the decimal point
incrementing
increasing a variable’s value by 1