Variables
- String: text, images, messages
- Integer: number without decimals
- Float: number with decimals
- print(type(variable)) in order to determine the type of the variable.
Variable type conversion
- Cannot add two different types of variables (e.g. one cannot add string and integer/float), so therefore conversion is necessary.
- Convert to:
o String (str(variable))
o Integer (int(variable))
o Float (float(variable))
Rules
- Separate by a comma (,)
Inputs
- input: requires user to fill data
- Asking the user to type a value, reading from a file, reading a network connection, accessing a database.
- Store the result in a variable. The variable type will be a string. One may need to convert the variable into an integer or a float.
Operations
- Standard operators in order of priority:
o Power (**)
o Multiply (*)
o Divide (/)
o Plus (+)
o Minus (–)
- Parentheses are used to show priority.
Output
- What is presented to the user.
- print: displays text.
- Integers and floats do not use quotation marks, whereas strings do.
Conditions
- Doing different things depending on different values.
- If… then…, else….
- if condition:
print
- else:
print
print(“Ending statement”)
- Indentations: the spaces before print are mandatory.
- A condition is a calculation that has a Boolean result: it is either true or false.
o print(comparison)
o It will print either “true” or “false”
- Comparisons:
o < less than
o <= less than or equal
o > higher than
o >= higher than or equal
o == equal
o != not equal, indifferent
Multiple conditions
- When a and b are conditions:
o a and b: TRUE when both conditions are TRUE
o a or b: TRUE when any condition is TRUE.
, Control flow
- What instructions will be executed?
o Instructions run in sequence, line by line.
o In case of error, the code after the error is not executed,
o The code can be divided in blocks of code, blocks are indicated by the indentation.
Lines at the same indentation level belong to the same block.
Indentations should only happen when there is a need for a separate block: after if, for, while, defining a function, or a class. It
should not happen in the middle of a block, or it generates and issue.
Lists
- A list is a variable that contains not just one value, but multiple values.
- Values are called elements.
- Each element is stored at an index.
- Indexes start at 0.
- The number of elements is called length of the list.
- Create lists using [ ].
- Separate elements with (,).
- Do not call a variable “list”. It is a function name.
- Counting the number of elements in a list:
o len(the_list)
- Access:
o The first item has an index of 0.
o For convenience, the last item has an index of -1.
o If there are 5 items, the index goes from 0-4.
o Accessing element at index i (the_list [i]).
- Modification:
o Modification of the value of the element at index i.
o the_list[i] = new_value
- Concatenation:
o Putting two lists together.
o the_list1 + the_list2
o One can also add a list that contains only one string.
- Check if a value is in a list:
o The result is true or false.
o if value (not) in the_list:
print
Strings
- A list of characters, except no modification is possible.
For loops
- Print each letter in variable
for x in variable:
print (x)
- Print each item of list
for x in list:
print (x)
- Print a word multiple time
for x in list:
print (“Word”)
- Sum:
o Start with a total at 0.
o For each value in the list, add it to the total.
o for value in the_list.
o Example:
my_list = [1, 2, 3]
total = 0
for elt in my_list:
total = total + elt
print(total) if you want the total at the end of each element in the list
print(total) if you want the total at the end
Range
- Print numbers in a particular range
- for x in range(number):
print
Voordelen van het kopen van samenvattingen bij Stuvia op een rij:
Verzekerd van kwaliteit door reviews
Stuvia-klanten hebben meer dan 700.000 samenvattingen beoordeeld. Zo weet je zeker dat je de beste documenten koopt!
Snel en makkelijk kopen
Je betaalt supersnel en eenmalig met iDeal, creditcard of Stuvia-tegoed voor de samenvatting. Zonder lidmaatschap.
Focus op de essentie
Samenvattingen worden geschreven voor en door anderen. Daarom zijn de samenvattingen altijd betrouwbaar en actueel. Zo kom je snel tot de kern!
Veelgestelde vragen
Wat krijg ik als ik dit document koop?
Je krijgt een PDF, die direct beschikbaar is na je aankoop. Het gekochte document is altijd, overal en oneindig toegankelijk via je profiel.
Tevredenheidsgarantie: hoe werkt dat?
Onze tevredenheidsgarantie zorgt ervoor dat je altijd een studiedocument vindt dat goed bij je past. Je vult een formulier in en onze klantenservice regelt de rest.
Van wie koop ik deze samenvatting?
Stuvia is een marktplaats, je koop dit document dus niet van ons, maar van verkoper bastudent. Stuvia faciliteert de betaling aan de verkoper.
Zit ik meteen vast aan een abonnement?
Nee, je koopt alleen deze samenvatting voor €5,49. Je zit daarna nergens aan vast.