Practice

text = "Data Carpentry"
type(text)
## <type 'str'>
text
## 'Data Carpentry'
print(text)
## Data Carpentry
2+2
## 4
6*7
## 42
2**16
## 65536
13%5
## 3

Logic operators

3 > 4
## False
True and True
## True
True and False
## 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

rev
## {'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