# Week 1, Basics of Python3
## Part2 Sequence Objects
### 1.2.3 Tuples
Tuples are **immutable** sequences typically used to store heterogeneous data.
The best way to view tuples is as a single object that consists of several different parts.
**Construct a tuple**
```python
T = (1,3,5,7)
len(T)
Out: 4
```
**Packing tuples**
```python
x = 12.23
y = 23.34
coordinate = (x,y)
type(coordinate) //check type of coordinate
Out:tuple
coordinate
Out:(12.23,23.34)
```
**Unpacking tuples**
```python
(c1, c2) = coordinate
c1
out: 12.23
```
**Question 2**
Consider **x=(1,2,3) **. Use the **count **method to count the number of **3 **s in **x **.
```python
x.count(3)
```
**Question 3**
Consider again **x=(1,2,3) **. Sum the numbers in **x **using a single function.
```python
sum(x)
```
### 1.2.4 Ranges
Ranges are immutable sequences of integers, and they are commonly used in for loops.
**Create a range object**
```python
range(5)
Out: range(0,5)
In list (range(5))
Out: [0,1,2,3,4]
```
**Create a range with step of 2**
```python
list(range(1,13,2))
Out: [1,3,5,7,9,11]
```
To store a range object, Python is only storing three different numbers, which are the starting number, the stopping number, and its step size. If you have a very large dataset that contains, say, 10 million objects, if you first create a list that contains the indices for accessing these 10 million numbers, you've just wasted a lot of space. **Consequently, use range objects as is. Don't turn them into lists before using them.**
### 1.2.5 Strings
Strings are immutable sequences of characters. Because strings are immutable objects, Python doesn't actually modify your string. Instead what it does -- it returns a new string to you.
**Create a string**
```python
S = "Python"
S[0:3]
Out: "Pyt"
```
**Adding two strings**
```python
"hello" + "world"
Out: "helloworld"
```
these two objectshave to be of the same type.
**Concatenate**
```python
S = "Python"
3 * S
Out: "PythonPythonPython"
```
**Concatenate a number and a string**
first turn the number into a string with `str()` function
```python
"eight equals" = + str(8)
```
### 1.2.6 Sets
Sets are **unordered collections** of distinct hashable objects. You can use sets for immutable objects like numbers and strings, but not for mutable objects like lists and dictionaries.
There are two types of sets: "set" and "frozen set". The difference between these two is that a frozen set is immutable once it has been created.
One of the key ideas about sets is that they cannot be indexed. So the objects inside sets don't have locations.
Another key feature about sets is that the elements can never be duplicated.So if you have a given element or object in your set, say number 3,
if you try adding that number again in the set, nothing happens.This means that all of the objects inside a set are always going to be **unique or distinct.**
**Creating a set**
```python
ids = set() //created an empty set named "ids"
ids = set([1,2,3,4,6,7,8,9])
```
**Adding members to the set**
```python
ids.add(10) //adding another member of the set
ids
Out: {1,2,3,4,6,7,8,9}
ids.add(2) // nothing happens cause 2 is already a member of the set
```
**Creating a set with `range()`,***
```python
ids = set(range(10))
ids
Out: {0,1,2,3,4,5,6,7,8,9}
```
**Operations of two sets**
```python
ids = set(range(10))
males = set([1,3,5,6,7])
females = ids - males
females
Out: {0,2,4,8,9}
// Union operation //
everyone = male | female
// Intersection
everyone & set([1,2,3])
Out: {1,2,3}
```
### 1.2.7 Dicstionaries
Dictionaries are mappings from **key objects** to **value objects**.
Dictionaries consists of **Key:Value** pairs, where the keys must be **immutable** and the values can be **anything**.
Dictionaries can be used for performing very fast look-ups on unordered data.
**Construct a dictionary**
```python
age = dict{}
age = {"Tim": 28, "Jim": 31, "Pam": 21, "Sam": 35}
```
**Looking up values in the dictionary**
```python
age["Pam"]
Out:21
```
**Modifying values in the dictionary**
```python
age["Tim"] = age["Tim"] +1
age["Tim"] += 1
```
**Viewing contents of the dictionary**
We can use methods `age.keys` and `age.values` to view all the keys/values in a dictionary. Upon the call of such request, Python returns to you an object which has a very special type. The type of the returned object is what's called a **"view object"**. They provide a dynamic view of the keys or values in the dictionary. A key point here is that as you update or modify your dictionary, the views will also change correspondingly.