Basic Python

update, islower, if in, abs, reverse, pprint, add, discard, union, intersection, symmetric_difference, issubset, issuperset, isdisjoint, enumerate

Naranjito 2021. 5. 18. 12:03
  • update

It takes a key-value pair and puts into the existing dictionary.

icecream = {'탱크보이': 1200, '폴라포': 1200, '빵빠레': 1800, '월드콘': 1500, '메로나': 1000}
new_product = {'팥빙수':2700, '아맛나':1000}
icecream.update(new_product)
icecream
>>>
{'탱크보이': 1200,
 '폴라포': 1200,
 '빵빠레': 1800,
 '월드콘': 1500,
 '메로나': 1000,
 '팥빙수': 2700,
 '아맛나': 1000}

 

  • islower

Checks if all the characters in the text are in lower case.

user_input=input()
if user_input.islower():
    print(user_input.upper())
else:
    print(user_input.lower())

 

  • if in

If a particular value is present after in, it returns True.

disits_3=str(user_input[:3])
if disits_3 in ['010','011','012']:
    print('강북구')
else:
    pass
>>>
강북구

 

  • abs

Absolute, it returns the absolute value of the fiven number.

 

  • reverse

It reverses the elements of the list.

a=['1','2','3']
a.reverse()
a
>>>
['3', '2', '1']

 

  • pprint

Pretty-Print, for producing aesthetically representations of your data structures. The output is keep on a single line.

from pprint import pprint as pp 
n={'alice': [1, 2, 3], 'bob': 20, 'tony': 15, 'suzy': 30,"dodo": [1,3,5,7], "mario": "pitch"}
pp(n)
>>>
{'alice': [1, 2, 3],
 'bob': 20,
 'dodo': [1, 3, 5, 7],
 'mario': 'pitch',
 'suzy': 30,
 'tony': 15}

 

  • add

Add the element to the set.

k={100,200}
k.add(300)
k
>>>
{100, 200, 300}

 

  • discard

Eliminate the element from the set.

k={100,200}
k.discard(100)
k
>>>
{200, 300}

 

  • | or union

Sum of sets operator.

k={200,300}
m={1,2,3}
a=k|m 
a
>>>
{1, 2, 3, 200, 300}

 

  • & or intersection

Intersection of sets operator.

 

  • ^ or symmetric_difference

(Sum of sets)-(Intersection of sets)

 

  • issubset

Return True if all items in set x are present in set y.

x={"a", "b", "c"}
y={"f", "e", "d", "c", "b", "a"}
x.issubset(y)
>>>
True

 

  • issuperset

Return True if all items in the specified set exists in the original set.

x={"a", "b", "c"}
y={"f", "e", "d", "c", "b", "a"}
y.issuperset(x)
>>>
True

 

  • isdisjoint

Return True if two sets have not intersection.

x={"a", "b", "c"}
y={"f", "e", "d", "c", "b", "a"}
x.isdisjoint(y)
>>>
False

 

  • enumerate

Return a counter and the value from the iterable as a tuple at the same time.

for i in enumerate(list(range(0,11,2))):
    print(i)
>>>
(0, 0)
(1, 2)
(2, 4)
(3, 6)
(4, 8)
(5, 10)
for i, letter in enumerate(['A', 'B', 'C'], start=1):
   print(i, letter)
>>>
   
1 A
2 B
3 C

 

  • range

(start, end-1, width)

for i in range(2002,2051,4):
  print(i)
>>>
2002
2006
2010
2014
2018
2022
2026
2030
2034
2038
2042
2046
2050