Day 32 of #100DaysOfLearning
100 Days of Python

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

  • Creation: Lists can be created by placing comma-separated values inside square brackets [], or by using the list() function.

mylist = [1, 2, 3, 4, 5]  # Using square brackets
mylist2 = list((1, 2, 3, 4, 5))  # Using list() function
        

  • Indexing: Lists use zero-based indexing. You can access list items by referring to the index number.

print(mylist[0])  # Output: 1
        

  • Mutability: Lists are mutable, which means you can change their content.

mylist[0] = 10
print(mylist)  # Output: [10, 2, 3, 4, 5]
        

  • Methods: Lists have several methods for manipulating their contents:append(x): Adds an item x to the end of the list.extend(iterable): Adds all the elements of an iterable (like list, tuple, string) to the end of the list.insert(i, x): Inserts an item x at a given position i.remove(x): Removes the first item from the list whose value is x.pop([i]): Removes the item at the given position in the list, and returns it. If no index is specified, it removes and returns the last item in the list.index(x, [start], [end]): Returns the position of the first list item whose value is x.count(x): Returns the number of times x appears in the list.sort(key=None, reverse=False): Sorts the items in the list in ascending order by default. key and reverse can be used to customize the sort order.reverse(): Reverses the order of the list.clear(): Removes all items from the list.

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

  • Creation: Tuples can be created by placing comma-separated values inside round brackets ().

mytuple = (1, 2, 3, 4, 5)
        

  • Indexing: Tuples use zero-based indexing. You can access tuple items by referring to the index number.

print(mytuple[0])  # Output: 1
        

  • Immutability: Tuples are immutable, which means you cannot change their content.

mytuple[0] = 10  # This will raise an error
        

  • Methods: Tuples have two built-in methods:count(x): Returns the number of times x appears in the tuple.index(x): Returns the position of the first tuple item whose value is x.
  • Usage: Because tuples are immutable, they are often used for values that should not and do not need to change. Examples could be days of the week, or dates on a calendar.

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

  • Creation: Dictionaries can be created by placing comma-separated key-value pairs inside curly braces {}. Each key-value pair is separated by a colon :⁴.

mydict = {'name': 'John', 'age': 30, 'city': 'New York'}
        

  • Accessing Values: You can access the items of a dictionary by referring to its key name.

print(mydict['name'])  # Output: John
        

  • Updating Values: Dictionaries are mutable, which means you can change their content.

mydict['age'] = 25
print(mydict)  # Output: {'name': 'John', 'age': 25, 'city': 'New York'}
        

  • Methods: Dictionaries have several methods for manipulating their contents⁴:keys(): Returns a new object that displays a list of all the keys in the dictionary.values(): Returns a new object that displays a list of all the values in the dictionary.items(): Returns a new object that displays a list of the dictionary's key-value tuple pairs.get(key[, default]): Returns the value for a key if it exists in the dictionary. If not, it returns a default value.update([other]): Adds key-value pairs from a second dictionary object to the first, overwriting values of the same key.pop(key[, default]): Removes a key from a dictionary, if it is present, and returns its value.popitem(): Removes and returns a (key, value) pair from the dictionary. Pairs are returned in LIFO (Last In, First Out) order.clear(): Removes all items from the dictionary.

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

  • Creation: Sets can be created by placing comma-separated values inside curly braces {}⁴.

myset = {1, 2, 3, 4, 5}
        

  • Uniqueness: Sets automatically remove duplicate values.

myset = {1, 2, 2, 3, 4, 4, 5, 5}
print(myset)  # Output: {1, 2, 3, 4, 5}
        

  • Immutability: While sets themselves can be modified, the elements placed in sets must be of immutable type (e.g., numbers, strings, tuples).
  • Methods: Sets have several methods for manipulating their contents⁴:add(x): Adds an element x to the set.remove(x): Removes element x from the set. Raises a KeyError if the element is not found.discard(x): Removes element x from the set if it is present.pop(): Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.clear(): Removes all elements from the set.
  • Set Operations: Python also provides a variety of operations for performing common set operations⁴.union(): Returns a set containing the union of sets.intersection(): Returns a set containing the intersection of sets.difference(): Returns a set containing the difference of sets.symmetric_difference(): Returns a set containing the symmetric difference of sets.



To view or add a comment, sign in

More articles by Shinya Yanagihara

  • Day 100 of #100DaysOfLearning

    I have mixed feelings about it, as if it was long and short. This is finally the 100th activity that I started with the…

    1 Comment
  • Day 99 of #100DaysOfLearning

    What a surprise! I found myself on the 99th day of the 100Days of Learning activity. Continuation is power, indeed.

  • Day 98 of #100DaysOfLearning

    How do you take notes when you study? There are some note-taking systems and techniques, such as Cornell note-taking…

  • Day 97 of #100DaysOfLearning

    Today is the fourth day of setting up a Windows environment. Today I finally get to set up my long-awaited development…

  • Day 96 of #100DaysOfLearning

    I am sure you are all aware that open source also has a license. I knew that, but I always managed my GitHub…

  • Day 95 of #100DaysOfLearning

    Today is the third day of building a new PC environment. Today I was mainly working on the configuration of Visual…

    2 Comments
  • Day 94 of #100DaysOfLearning

    It is no exaggeration to say that Windows is now Linux. I'm sure some of you don't know what I mean.

    2 Comments
  • Day 93 of #100DaysOfLearning

    In order to make a clean break with the past, I did a clean install of Windows 11 and began to create a clean…

  • Day 92 of #100DaysOfLearning

    Happy April Fool's Day! Today is April 1, which is April Fool's Day. Some of you may have been looking forward to April…

  • Day 91 of #100DaysOfLearning

    I actually haven't used a Mac since I left my last job and entered my career break period. I use Windows every day.

Others also viewed

Explore content categories