Skip to content

Instantly share code, notes, and snippets.

@rolroralra
Last active December 21, 2020 13:11
Show Gist options
  • Save rolroralra/88962c332bbf7e0f8bb6a129525825f6 to your computer and use it in GitHub Desktop.
Save rolroralra/88962c332bbf7e0f8bb6a129525825f6 to your computer and use it in GitHub Desktop.
Python

Python

https://www.youtube.com/playlist?list=PLnIaYcDMsScyhT18mwY71rV_aHdP-OhLd


Pandas

https://www.youtube.com/playlist?list=PLVNY1HnUlO26Igldy2Q6Nb2LZbpQWTyle


Python Http Server

$ python -m http.server ${SERVER_PORT}      # python3

$ python -m SimpleHTTPServer ${SERVER_PORT}  # python2

Print without space delimiter and new line

print('''Hello
My
Wolrd~''', end='', sep='')

How to print number with commas as thousands separators?

Details

value_info = {
  "Seoul": [10312545, 91375],
  "Pusan": [3567910, 5868],
  "Incheon": [2758296, 64888],
  "Daegu": [2511676, 17230],
  "Gwangju": [1454636, 29774],
}

for key in value_info:
    print(f"{key.rjust(15)}"
          f"{f'{value_info[key][0]:,d}'.rjust(15)}"
          f"{(('+' if value_info[key][1] >= 0 else '-') + f'{value_info[key][1]:,.0f}').rjust(15)}"
          , sep="")


String Format -- %10d, %10.4f, %10s (Python)

Details

# 1. %-formatting
weight = 79.12
print("%-10.4f" % weight)

# 2. format function
print(format(weight, "-10.4f"))

# 3. String 
hash_value = "1234567890"
print("%13s" % hash_value)
# print(format(hash_value, "%13s"))   # ValueError: Invalid format specifier
print(hash_value.rjust(13))


F String Format (Python)

Details

# 1. %-formatting
arr=[1,2,3]
print("%s %s %s" % (arr[0], arr[1], arr[2]))

truple=(1,2,3)
print("%s %s %s" % truple)


# 2. str.format()
name = "rolroralra"
age = 20
print("Hello, {}. I am {}.".format(name, age))
print("Hello, {1}. You are {0}.".format(age, name))

person = {'name': 'Eric', 'age': 74}
print("Hello, {name}. You are {age}.".format(name=person['name'], age=person['age']))

# You can also use ** to do this neat trick with dictionaries
print("Hello, {name}. You are {age}.".format(**person))


# 3. f string
print(f"Hello, {name}. You are {age}.")


Python Operator

https://docs.python.org/ko/3.7/library/operator.html

Ternary Operator (3항 연산자)

Details

a = 10
b = 10

# Old Version Ternary Operation (A and B or C)
print(a == b and "TRUE" or "FALSE")
# OUTPUT: TRUE
print(a == b and a - b or a + b)    # This old version ternary operator has this problem
# OUTPUT: 20

# New Ternary Operation in python 2.5
print("TRUE" if a == b else "FALSE")
# OUTPUT: TRUE
print(a - b if a == b else a + b)
# OUTPUT: 0


Set

Details

set1 = {1,2,3}
set2 = {3,4,5}
print(set1 & set2)
#print(set1.intersection(set2))
print(set1 | set2)
#print(set1.union(set2))
print(set1 - set2)
#print(set1.difference(set2))
print(set1 ^ set2)
#print(set1.symmetric_difference(set2))


Python List Comprehension

a = [2, 6, 7, 8, 9]
list_even = list()
for num in a:
    if num % 2 ==0:
        list_even.append(num)
#       list_even += [num]
#       list_even.extend([num])
print(list_even)

# List Comprehension
print([num for num in a if num % 2 == 0])

Difference between == and is

https://www.tutorialspoint.com/difference-between-and-is-operator-in-python

Details

# Python program to  
# illustrate the  
# difference between 
# == and is operator 
# [] is an empty list 
list1 = [] 
list2 = [] 
list3=list1 
  
if (list1 == list2): 
   print("True") 
else: 
   print("False") 
# True

  
if (list1 is list2): 
   print("True") 
else: 
   print("False") 
# False


if (list1 is list3): 
   print("True") 
else:     
   print("False")
# True


Python Comprehension (TODO)

https://mingrammer.com/introduce-comprehension-of-python/


Python Lambda (TODO)

Details

import fucntools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment