W0 Flashcards
How do you display something?
print()
What is the code comment?
”#”
- Anything to the right of the # will be ignored
- Can use code comments is adding a general description
What are the symbols for the different mathematical operations?
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponential (to the Power of…)
How do you assign a value to a variable and return it?
- First define the variable name and variable (i.e. the variable name IS tree; the variable is 20):
tree = 20
- You can then return the variable via the variable name:
print(result)
What type of Variable Names are invalid?
- Starts with a number (1_data)
- Has a gap/white space (new data)
- Has an apostrophe (john’s_salary)
- Has a hyphen (old-data)
- Has the $ symbol (price_in_$)
How can you update the value stored for a variable?
- You can update the code sequentially:
x = 30
print(x)
x = 70
print(x)
- You can use arithmetic operations:
x = 30
print(x)
print(x + 70)
x = x + 70
print(x)
- You can use a shortcut of +=:
x = 30
x += 70
print(x)
What are the different syntax shortcuts to update the value stored for a variable?
For a defined variable x, you can update the value stored with:
+= Adds
-= Subtracts
*= Multiplies
/= Divides
**= Exponents
How can you determine the type of data used?
You can use the command:
type()
- By coding print(type(x)), you are able to confirm the variable type
What are the different data types?
int (Integer values)
float (Decimal numbers)
str (Strings/words)
How do you convert a float to an integer and vice versa?
- To convert an integer to a float:
float()
EXAMPLE: print(float(10))
- To convert a float to an integer:
int()
EXAMPLE: print(int(4.3))
- NOTE: This will always round down the decimal, even if the number after the decimal point is greater than 5
How do you round a number?
Use the command:
round()
How do you return text?
Using strings, you can use double quotation marks (“ “) or single quotation marks (‘ ‘)
- Can define variables that are words:
fb_1 = “Facebook”
Why would you alternate between single quotation marks and double quotation marks?
If you want to have quotation marks/apostrophes contained within a string
What does the \ character perform in a string?
The \ character has a special function within a string it escapes (cancels) the special function of characters. You need to also include the apostrophe/speech marks after the character: i.e. ' and "
EXAMPLE:
motto = ‘Facebook's old motto was “move fast and break things”.’
print(motto)
RESULT: Facebook’s old motto was “move fast and break things”.
What is concatenation?
You can link two or more distinct strings using the + operator or the * operator.
EXAMPLE: + operator
print(‘a’ + ‘b’)
print(‘a’ + ‘ ‘ + ‘b’)
print(‘This’ + ‘is’ + ‘a’ + ‘sentence.’)
Output
ab
a b
Thisisasentence.
EXAMPLE: * operator
print(‘a’ * 1)
print(‘a’ * 4)
print(‘a ‘ * 5)
Output
a
aaaa
a a a a a
How can you concatenate between integers and floats?
We can use the int() or float() command to convert a string of type str to a number of type int or float.
print(int(‘4’) + 1)
print(float(‘3.3’) + 1)
Output
5
4.3
What do triple quotation marks allow you to do?
Using triple quotation marks also allows us to use both single and double quotation marks without needing to escape them.
EXAMPLE:
motto = ‘'’Facebook’s old motto was ‘move fast and break things’.’’’
print(motto)
Output
Facebook’s old motto was ‘move fast and break things’.
How are data points and datasets connected?
Each value in a table is a ‘data point’.
A collection of data points makes up a ‘dataset’.
How do you create a list?
To create a list, we do the following:
- Separate the data points with a comma
*Surround the sequence with square brackets
EXAMPLE:
row_1 = [‘Facebook’, 0.0, ‘USD’, 2974676, 3.5]
print(row_1)
What command can be used to determine the length of a list?
len()
EXAMPLE:
list_1 = [1, 6, 0]
print(len(list_1))
list_2 = []
print(len(list_2))
Output
3
0
What is the index number of a list?
The specific number associated with each element (data point) in a list.
The indexing always starts at 0, so the first element will have the index number 0
What is the command to find the index number of a list?
variable[x]
where x is the index number.
EXAMPLE. We can retrieve the first element (the string ‘Facebook’) with the index number 0 by running the code print(row_1[0])
row_1 = [‘Facebook’, 0.0, ‘USD’, 2974676, 3.5]
print(row_1[0])
Output
Facebook
What are the two different types of indexing?
- Positive indexing: the first element has the index number 0, the second element has the index number 1, and so on.
- Negative indexing: the last element has the index number -1, the second to last element has the index number -2, and so on.
What is list slicing?
When we select the first n elements (n stands for a number) from a list named a_list, we can use the syntax shortcut a_list[0:n]