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
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller bastudent. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $5.99. You're not tied to anything after your purchase.