You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: welcome/Hello_Python.md
+333Lines changed: 333 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -485,6 +485,339 @@ for i in range(5, -1, -1):
485
485
...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 isall about using container data types!
486
486
487
487
## 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 tryand 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`isin 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
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 inzip(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'notin spam
655
+
```
656
+
657
+
```python
658
+
'cat'notin 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 listin 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.
0 commit comments