-
+ -
- -
* -
/ -
%modulo -
myvar *= 0.990% of myvar -
myvar -= 3subtract 3 from myvar
mynum = 1234intintegerfloatfloattypecheck type of objectmynum = 1.3creates a float variablebolTrue or False- types can be converted
str()convert to stringfloat()convert to float- ...
mystring = "Test String"len(mystring)length of stringmystring[0]- python uses indexing for string [0] returns the first characterprint("var1 = {} and var2 = {}".format(var1,var2))print format strintsplitcan be used to split strings into lists
- Use
bpythonto test out little snippets of code. In comparison to python's standard interactive shell it offers command completion and function parameter view. - Use https://www.hackerrank.com/ for coding challenges
-
Sample liste:
list_of_random_things = [1, 3.4, 'a string', True] -
Lists can be accessed with
list[0]up untillist[len(list)-1] -
When using slicing, it is important to remember that the lower index is inclusive and the upper index is exclusive.
Therefore, this:
list_of_random_things = [1, 3.4, 'a string', True] list_of_random_things[1:2] [3.4]
- Lists are mutable
len()length of listmax()max element of listmin()smallest element of listsorted()sorted listjoin()join list elemnts to stringappend()add something to a list
- Store related types of information
- Ordered collection of objects
- Immutable (no adding or removing items)
- Saves processing power
dimensions = 10, 20, 30 # assigning a tuple
length, width, height = dimensions # unpacking- Sets contain unique elemnts
addadds elements to a list- Sets are unordered
pop()removes one random element from the set- Sets are like mathematical sets. They support fast intersect, union operators.
- Store pairs
- key and value store
- keys do not need to be from the same datatype
- lookup of values can be done using
dict["lookupval"]or dict.get["lookupval"]. Get returnsNoneif the lookup failes, a lookup with [] returns an error.
fruit_weight = {"apple": 5, "banana": 6}
fruit_weight["apple"]