Code Monkey home page Code Monkey logo

algorithm_python's Introduction

Template Code

import sys

sys.stdin = open('sample_input.txt')
input = sys.stdin.readline

if __name__ == '__main__':

    test_case_total_count = int(input())

    for test_case in range(test_case_total_count):
        answer = int(input())

        # TODO: Implementation

        print(f'#{test_case + 1} {answer}')

Print without space delimiter and new line (String Literal)

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 functools

algorithm_python's People

Contributors

rolroralra avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.