Practice
text = "Data Carpentry"
type(text)
## <type 'str'>
## 'Data Carpentry'
## Data Carpentry
## 4
## 42
## 65536
## 3
Logic operators
## False
## True
## False
Lists
numbers = [1,2,3]
numbers[0]
## 1
for num in numbers:
print(num)
## 1
## 2
## 3
Methods
numbers.append(4)
print(numbers)
## [1, 2, 3, 4]
Tuples use parentheses
a_tuple = (1, 2, 3)
another_tuple = ('blue', 'green', 'red')
Dictionaries
translation = {'one': 'first', 'two': 'second'}
translation['one']
## 'first'
rev = {'first': 'one', 'second': 'two'}
rev['first']
## 'one'
rev['third'] = 'three'
rev
## {'second': 'two', 'third': 'three', 'first': 'one'}
For loops for dictionaries
for key, value in rev.items():
print(key, '->', value)
## ('second', '->', 'two')
## ('third', '->', 'three')
## ('first', '->', 'one')
for key in rev.keys():
print(key, '->', rev[key])
## ('second', '->', 'two')
## ('third', '->', 'three')
## ('first', '->', 'one')
Exercise
## {'second': 'two', 'third': 'three', 'first': 'one'}
rev['second'] = '2'
print(rev)
## {'second': '2', 'third': 'three', 'first': 'one'}
Functions
def add_function(a, b):
result = a + b
return result
z = add_function(20, 22)
print(z)
## 42