Day 32 of #100DaysOfLearning
Today was a weekend, so we took it easy and rested. And I was working on a proposal for a technical conference. It was right before the deadline.
So, as far as learning goes, I only learned the basics of Python lightly. However, every day is like Sunday for me now, lol.
100days-of-python-with-jupyter/works/Day002.ipynb at main · shinyay/100days-of-python-with-jupyter (github.com)
Day 2
Data Structures
List
In [4]:
## List Basics
month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
print(month)
print(month[0])
print(month[11])
print(month[0:4])
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
January
December
['January', 'February', 'March', 'April']
In [8]:
## Slice
number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(number[::2])
print(number[1::2])
print(number[::3])
[1, 3, 5, 7, 9]
[2, 4, 6, 8, 10]
[1, 4, 7, 10]
In [14]:
## List and Functions
print('len:', len(number))
print('type:', type(number))
print('list', list('ABCDEFG'))
len: 10
type: <class 'list'>
lsit ['A', 'B', 'C', 'D', 'E', 'F', 'G']
In [15]:
## List nesting
odd = [1, 3, 5, 7, 9]
even = [2, 4, 6, 8, 10]
num_list = [odd, even]
print(num_list)
[[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]
In [22]:
## Insert/Delete by method
number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
number.append(20)
print(number)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20]
In [26]:
number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(number.pop())
print(number)
10
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In [31]:
number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
number.insert(0, -1)
print(number)
number.remove(-1)
print(number)
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [35]:
## Merging Lists
odd = [1, 3, 5, 7, 9]
even = [2, 4, 6, 8, 10]
number = odd + even
print(number)
number.extend(even)
print(number)
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10, 2, 4, 6, 8, 10]
In [38]:
## Index
month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
print(month.index('May'))
4
In [48]:
## Index
hello_list = list('Hello Python')
print(hello_list)
print(hello_list.count('o'))
['H', 'e', 'l', 'l', 'o', ' ', 'P', 'y', 't', 'h', 'o', 'n']
2
In [50]:
hello_list = 'Hello Python'.split(' ')
print(hello_list)
hello = ','.join(hello_list)
print(hello)
['Hello', 'Python']
Hello,Python
Python Lists
In Python, a list is a built-in data structure that is mutable, meaning it can be changed. It's an ordered sequence of elements, and each element or value inside a list is called an item. Lists are defined by having values between square brackets [].
Key Points about Python Lists
mylist = [1, 2, 3, 4, 5] # Using square brackets
mylist2 = list((1, 2, 3, 4, 5)) # Using list() function
print(mylist[0]) # Output: 1
Recommended by LinkedIn
mylist[0] = 10
print(mylist) # Output: [10, 2, 3, 4, 5]
Tuples
In [63]:
number_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(number_tuple)
print(type(number_tuple))
print(number_tuple[0])
print(number_tuple.index(1))
another_tuple = 1, 2, 2, 3, 3, 3
print(another_tuple.count(3))
xy = ('A', 'B')
x, y = xy
print(xy)
print(x)
print(y)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
<class 'tuple'>
1
0
3
('A', 'B')
A
B
Python Tuples
In Python, a tuple is a built-in data structure that is immutable, meaning it cannot be changed. It's an ordered sequence of elements, and each element or value inside a tuple is called an item. Tuples are defined by having values between round brackets ()³.
Key Points about Python Tuples
mytuple = (1, 2, 3, 4, 5)
print(mytuple[0]) # Output: 1
mytuple[0] = 10 # This will raise an error
Directory
In [68]:
dic = {'a': 1, 'b': 2}
print(dic)
print(type(dic))
another_dic = dict(x = 10, y = 20)
print(another_dic)
{'a': 1, 'b': 2}
<class 'dict'>
{'x': 10, 'y': 20}
In [70]:
print(dic['a'])
dic['c'] = 3
print(dic)
print(dic.keys())
print(dic.values())
1
{'a': 1, 'b': 2, 'c': 3}
dict_keys(['a', 'b', 'c'])
dict_values([1, 2, 3])
Python Dictionaries
In Python, a dictionary is a built-in data structure that is mutable, meaning it can be changed. It's an unordered collection of key-value pairs, where each key must be unique⁴. Dictionaries are defined by having key-value pairs between curly braces {}⁴.
Key Points about Python Dictionaries
mydict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(mydict['name']) # Output: John
mydict['age'] = 25
print(mydict) # Output: {'name': 'John', 'age': 25, 'city': 'New York'}
Set
In [74]:
number_set = {1, 1, 1, 1, 2, 2, 2, 3, 3, 3}
print(number_set)
print(type(number_set))
{1, 2, 3}
<class 'set'>
The & operator is used to perform a bitwise AND operation between two integers. The | operator is used to perform a bitwise OR operation between two integers. The ^ operator is used to perform a bitwise XOR operation between two integers.
In [78]:
first_set = {'A', 'B', 'B', 'C', 'C', 'C'}
second_set = {'C', 'C', 'C', 'D', 'D'}
print(first_set - second_set)
# AND
print(first_set & second_set)
# OR
print(first_set | second_set)
# XOR
print(first_set ^ second_set)
{'B', 'A'}
{'C'}
{'D', 'A', 'C', 'B'}
{'B', 'D', 'A'}
Python Sets
In Python, a set is a built-in data structure that represents an unordered collection of unique elements. Sets are defined by having values between curly braces {}⁴.
Key Points about Python Sets
myset = {1, 2, 3, 4, 5}
myset = {1, 2, 2, 3, 4, 4, 5, 5}
print(myset) # Output: {1, 2, 3, 4, 5}
Let's connect