Back to the Basics - Python

markkimjr·2021년 1월 17일
0
post-thumbnail

Data Types:

  • Integer - 1, 2, 3, 4, 5
  • FLoat - 1.0, 2.0, 3.0, 4.0, 5.0
  • Complex Numbers - 1 + 3j (in python, use j to represent complex numbers)
  • String - "hellooo world"
  • Boolean - True/False

Python Order of Operations:

  1. ( )
  2. **
  3. *, / , 그리고 %
    • 와 -

Lists

#Retrieving items within a nested list:

any_list = [
  [1,2,3],
  [4,5,6],
  [7,8,9],
  [[10,11,12], 13, 14]
];
any_list[3]; # equals [[10,11,12], 13, 14]
any_list[3][0]; # equals [10,11,12]
any_list[3][0][1]; # equals 11

#list.append()

.append() is a method that adds an item to the end of a list

arr = [1,2,3];
arr.append(4); # arr is now [1,2,3,4]

#list.pop()

.pop() is a method that "pop"s the last item of a list. You can store this popped item in variable

three = [1, 4, 6];
remove_one = three.pop();
print(remove_one); # Returns 6
print(three); # Returns [1, 4]

#Adding lists together

You can use "+" to add lists together. Just make sure to reassign the new list to a var

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7]
 
list1 + list2
 
print(list1)
> [1, 2, 3, 4]
 
list1 = list1 + list2
 
print(list1)
> [1, 2, 3, 4, 5, 6, 7]

#list.insert()

.insert() is a method that inserts an item into a list according to the desired list index

goats = ["kobe", "mj", "magic"]
goats.insert(2, "larry")
print(goats)

["kobe", "mj", "larry", "magic"]

#Amending lists

Use the index number of list items to amend specific items in a list

def merge_and_swap(list1, list2):
    list1 = list1 + list2
    list1[0], list1[-1] = list1[-1], list1[0]
    return list1

print(merge_and_swap([1, 2, 3, 4, 5], [6, 7, 8, 9]))
[9, 2, 3, 4, 5, 6, 7, 8, 1]

#Slicing lists

list_name[start: stop: step]

The third value (step) has a default value of 1. You can also work with a copy of a list using [:]. A key takeaway here is that when you slice a list, you don't actually edit the original list. Instead, you create a new one.

bts = ["RM", "제이홉", "진", "정국", "지민", "뷔", "슈가"]
sub_bts = bts[1:4]
 
print(f"bts = {bts}")
> bts = ['RM', '제이홉', '진', '정국', '지민', '뷔', '슈가']
print(f"sub_bts = {sub_bts}")
> sub_bts = ['제이홉', '진', '정국']

#Deleting items from a list

  1. del list_name[index#]
  2. using .remove(list-item)

Tuples

An important difference between lists and tuples is that tuples are immutable (items cannot be changed). You can still add tuples together and retrieve stored items in a tuple using the same methods used for lists

my_tuple = (1, 2, 3, 4, 5, 6, 7, 8)

new_tuple = my_tuple[:] + (5, 5, 5)
print(new_list)
(1, 2, 3, 4, 5, 6, 7, 8, 5, 5, 5)

Key takeaway: lists use more memory than tuples, so when dealing with items that do not need to be changed, use tuples instead of lists

Sets

Key takeaways:

  • items in a set do not get stored in order. When you go through set items using a for loop, the loop retrieves items randomly (not in order)
  • sets do not have indices (no order)
  • there cannot be more than one of the same item in a set. If there is already the same existing item in a set, the latest item replaces the former item

#Adding/removing items from a set

Use .add() to add an item to a set

my_set = {1, 2, 3}
my_set.add(4)
 
print(my_set)
> {1, 2, 3, 4}

Use .remove() to remove an item from a setmy_set = {1, 2, 3}

my_set.remove(3)
 
print(my_set)
> {1, 2}

#Set intersection & Union

"&" and intersection() show intersecting items between two sets

set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}
 
print(set1 & set2)
> {4, 5, 6}
 
print(set1.intersection(set2))
> {4, 5, 6}

"|" and .union() unites two sets together
print(set1 | set2)

> {1, 2, 3, 4, 5, 6, 7, 8, 9}
 
print(set1.union(set2))
> {1, 2, 3, 4, 5, 6, 7, 8, 9}

Dictionaries

Dictionaries consist of key: value pairs

dict1 = { 1 : "one", 1 : "two" }
print(dict1)
 
> { 1: "two" }

#Adding/editing dictionary items

dictionary_name[new_key] = new_value
my_dict = { "one": 1, 2: "two", 3 : "three" }
my_dict["four"] = 4
print(my_dict)
> {'one': 1, 2: 'two', 3: 'three', 'four': 4}

#Removing dictionary items

Use del

my_dict = { "one": 1, 2: "two", 3 : "three" }
 
del my_dict["one"]
 
print(my_dict)
> {2: 'two', 3: 'three'}

#Looping through a dictionary

Loop through a dictionary using .keys(), .values(), or .items()
.keys() - returns dict keys in a list
.values() - returns dict values in a list
.items() - returns key:value pairs in tuples (within a surrounding list)

my_dict = {"name": "mark", "age": 24, "location": "seoul"}

print(my_dict.keys())
print(my_dict.values())
print(my_dict.items())

dict_keys(['name', 'age', 'location'])
dict_values(['mark', 24, 'seoul'])
dict_items([('name', 'mark'), ('age', 24), ('location', 'seoul')])
profile
#mambamentality 🐍

0개의 댓글