Skip to content

Commit cb347d3

Browse files
Init section about lists.
1 parent b05673e commit cb347d3

File tree

1 file changed

+333
-0
lines changed

1 file changed

+333
-0
lines changed

welcome/Hello_Python.md

Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,339 @@ for i in range(5, -1, -1):
485485
...and that's it for control flow! Being able to use conditional statements like `if`, `elif`, and `else`, as well as being able to write loops will enable us to work with larger and larger datasets using Python. Speaking of which, let's see how Python represents large collections of data the next section, which is all about using container data types!
486486
487487
## Container Types
488+
We saw above that by using variables, we can save certain pieces of data in our Python program, to be used later. But, what if we suddenly had many different pieces of information to keep track of?
489+
```
490+
var1 = something
491+
var2 = something
492+
var3 = something
493+
var4 = something
494+
var5 = something
495+
var6 = something
496+
var7 = something
497+
...
498+
```
499+
You can imagine that it can quickly become impractical to try and keep track of all the data in our code only using variables. That is why Python provides us with certain *container types* - these are data types whose only job is to encapsulate lots of other values in them, all at the same time.
500+
501+
Let's go over the 4 most commonly used container types: lists, tuples, dictionaries, and sets.
502+
503+
### Lists in Python
504+
Lists (represented by the `list` type) are the most straightforward container type. They simply represent an *ordered sequence of values*.
505+
506+
Just as strings are a sequence of textual characters, the `list` in Python allows us to collect lots of different values altogether sequentially, enclosed in a pair of square brackets (`[]`). For example, consider the following list of animal names:
507+
```python
508+
['bat', 'cat', 'elephant', 'rat']
509+
```
510+
511+
### Getting Individual Values in a List with Indexes
512+
Because the data in a Python `list` is in a sequence, we can retrieve individual values in that `list` (aka an *element of the list*) if we know where it is positioned in that list.
513+
514+
Another name for the position of an element in a `list` is knowing the *index* of that element. These indices are *zero-indexed* as well, meaning the index position of the first element is `0`. The indices of the following elements incrementally increases by 1, as you can see in the examples that following:
515+
516+
```python
517+
spam = ['bat', 'cat', 'elephant', 'rat']
518+
spam[0] # result: 'bat'
519+
```
520+
521+
```python
522+
spam[1] # result: 'cat'
523+
```
524+
525+
```python
526+
spam[2] # result: 'elephant'
527+
```
528+
529+
```python
530+
spam[3] # result: 'rat'
531+
```
532+
533+
### Negative Indexes
534+
Python will also let us access elements from the back of the list, using negative index values. These begin with the value of `-1` for the very last element, and decrease by one as we move further to the left.
535+
536+
```python
537+
spam = ['cat', 'bat', 'rat', 'elephant']
538+
spam[-1] # result: 'elephant'
539+
```
540+
### Getting Sublists with Slices
541+
We can retrieve several items out of a `list` at once and put them in a new `list` by using a *slice*.
542+
543+
```python
544+
spam = ['cat', 'bat', 'rat', 'elephant']
545+
spam[0:4]
546+
```
547+
548+
```python
549+
spam[1:3]
550+
```
551+
552+
```python
553+
spam[0:-1]
554+
```
555+
556+
```python
557+
spam = ['cat', 'bat', 'rat', 'elephant']
558+
spam[:2]
559+
```
560+
561+
```python
562+
spam[1:]
563+
```
564+
565+
```python
566+
spam[:]
567+
```
568+
569+
### Getting a list Length with len
570+
571+
```python
572+
spam = ['cat', 'dog', 'moose']
573+
len(spam)
574+
```
575+
576+
### Changing Values in a List with Indexes
577+
578+
```python
579+
spam = ['cat', 'bat', 'rat', 'elephant']
580+
spam[1] = 'aardvark'
581+
spam
582+
```
583+
584+
```python
585+
spam[2] = spam[1]
586+
spam
587+
```
588+
589+
```python
590+
spam[-1] = 12345
591+
spam
592+
```
593+
594+
### List Concatenation and List Replication
595+
596+
```python
597+
[1, 2, 3] + ['A', 'B', 'C']
598+
```
599+
600+
```python
601+
['X', 'Y', 'Z'] * 3
602+
```
603+
604+
```python
605+
spam = [1, 2, 3]
606+
spam = spam + ['A', 'B', 'C']
607+
spam
608+
```
609+
610+
### Removing Values from Lists with del Statements
611+
612+
```python
613+
spam = ['cat', 'bat', 'rat', 'elephant']
614+
del spam[2]
615+
spam
616+
```
617+
618+
```python
619+
del spam[2]
620+
spam
621+
```
622+
623+
### Using for Loops with Lists
624+
625+
```python
626+
supplies = ['pens', 'staplers', 'flame-throwers', 'binders']
627+
628+
for i, supply in enumerate(supplies):
629+
print('Index {} in supplies is: {}'.format(str(i), supply))
630+
```
631+
632+
### Looping Through Multiple Lists with zip
633+
634+
```python
635+
name = ['Pete', 'John', 'Elizabeth']
636+
age = [6, 23, 44]
637+
638+
for n, a in zip(name, age):
639+
print('{} is {} years old'.format(n, a))
640+
```
641+
642+
### The in and not in Operators
643+
644+
```python
645+
'howdy' in ['hello', 'hi', 'howdy', 'heyas']
646+
```
647+
648+
```python
649+
spam = ['hello', 'hi', 'howdy', 'heyas']
650+
False
651+
```
652+
653+
```python
654+
'howdy' not in spam
655+
```
656+
657+
```python
658+
'cat' not in spam
659+
```
660+
661+
### The Multiple Assignment Trick
662+
663+
The multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:
664+
665+
```python
666+
cat = ['fat', 'orange', 'loud']
667+
size = cat[0]
668+
color = cat[1]
669+
disposition = cat[2]
670+
```
671+
672+
You could type this line of code:
673+
674+
```python
675+
cat = ['fat', 'orange', 'loud']
676+
size, color, disposition = cat
677+
```
678+
679+
The multiple assignment trick can also be used to swap the values in two variables:
680+
681+
```python
682+
a, b = 'Alice', 'Bob'
683+
a, b = b, a
684+
print(a)
685+
```
686+
687+
```python
688+
print(b)
689+
```
690+
691+
### Augmented Assignment Operators
692+
693+
| Operator | Equivalent |
694+
| ----------- | ----------------- |
695+
| `spam += 1` | `spam = spam + 1` |
696+
| `spam -= 1` | `spam = spam - 1` |
697+
| `spam *= 1` | `spam = spam * 1` |
698+
| `spam /= 1` | `spam = spam / 1` |
699+
| `spam %= 1` | `spam = spam % 1` |
700+
701+
Examples:
702+
703+
```python
704+
spam = 'Hello'
705+
spam += ' world!'
706+
spam
707+
```
708+
709+
```python
710+
bacon = ['Zophie']
711+
bacon *= 3
712+
bacon
713+
```
714+
715+
### Finding a Value in a List with the index Method
716+
717+
```python
718+
spam = ['Zophie', 'Pooka', 'Fat-tail', 'Pooka']
719+
spam.index('Pooka')
720+
```
721+
722+
### Adding Values to Lists with append and insert
723+
724+
**append()**:
725+
726+
```python
727+
spam = ['cat', 'dog', 'bat']
728+
spam.append('moose')
729+
spam
730+
```
731+
732+
**insert()**:
733+
734+
```python
735+
spam = ['cat', 'dog', 'bat']
736+
spam.insert(1, 'chicken')
737+
spam
738+
```
739+
740+
### Removing Values from Lists with remove
741+
742+
```python
743+
spam = ['cat', 'bat', 'rat', 'elephant']
744+
spam.remove('bat')
745+
spam
746+
```
747+
748+
If the value appears multiple times in the list, only the first instance of the value will be removed.
749+
750+
### Sorting the Values in a List with sort
751+
752+
```python
753+
spam = [2, 5, 3.14, 1, -7]
754+
spam.sort()
755+
spam
756+
```
757+
758+
```python
759+
spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
760+
spam.sort()
761+
spam
762+
```
763+
764+
You can also pass True for the reverse keyword argument to have sort() sort the values in reverse order:
765+
766+
```python
767+
spam.sort(reverse=True)
768+
spam
769+
```
770+
771+
If you need to sort the values in regular alphabetical order, pass str. lower for the key keyword argument in the sort() method call:
772+
773+
```python
774+
spam = ['a', 'z', 'A', 'Z']
775+
spam.sort(key=str.lower)
776+
spam
777+
```
778+
779+
You can use the built-in function `sorted` to return a new list:
780+
781+
```python
782+
spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
783+
sorted(spam)
784+
```
785+
786+
## Tuple Data Type
787+
788+
```python
789+
eggs = ('hello', 42, 0.5)
790+
eggs[0]
791+
```
792+
793+
```python
794+
eggs[1:3]
795+
```
796+
797+
```python
798+
len(eggs)
799+
```
800+
801+
The main way that tuples are different from lists is that tuples, like strings, are immutable.
802+
803+
## Converting Types with the list and tuple Functions
804+
805+
```python
806+
tuple(['cat', 'dog', 5])
807+
```
808+
809+
```python
810+
list(('cat', 'dog', 5))
811+
```
812+
813+
```python
814+
list('hello')
815+
```
816+
### Tuples in Python
817+
818+
### Dictionaries in Python
819+
820+
### Sets in Python
488821
489822
## Functions
490823

0 commit comments

Comments
 (0)