1. math library

# Ask Python to print sqrt(25) on line 

import math

print math.sqrt(25) 


2. sqrt 만 import 해오기 / 전부 import 해오기

# Import *just* the sqrt function from math on line 3!

from math import sort

from math import *


3. library import 해올 때는 되도록 import module 한 다음 module.function 해서 쓸 것. 함수명/변수명 중복되서 막 사용될 수 있음.

import math            # Imports the math module

everything = dir(math) # Sets everything to a list of things from math

print everything       # Prints 'em all!


4. python 에 기본적으로 내장되어있는 함수들 .upper(), .lower(), str(), len(), max(), min(), abs()

def biggest_number(*args):    #여러개의 인자를 넘기는 방법이 특이하다. 오오.

    print max(args)

    return max(args)

    

def smallest_number(*args):

    print min(args)

    return min(args)


def distance_from_zero(arg):

    print abs(arg)

    return abs(arg)



biggest_number(-10, -5, 5, 10)

smallest_number(-10, -5, 5, 10)

distance_from_zero(-10)


5. max()

# Set maximum to the max value of any set of numbers on line 3!


maximum = max(1, 19231, 2.5)


print maximum


6. min()

# Set minimum to the min value of any set of numbers on line 3!


minimum = min(4, -2 , 10, 123, -123.1)


print minimum


7. abs()


absolute = abs(-42)


print absolute


8. type()

# Print out the types of an integer, a float,

# and a string on separate lines below.


print type(42)

print type(-1.2)

print type('keol')


9. 

def shut_down(s):

    if s == "yes":

        return "Shutting down"

    elif s == "no":

        return "Shutdown aborted"

    else:

        return "Sorry"


10.

import math


print math.sqrt(13689)


11.

def distance_from_zero(arg):

    if type(arg) == int or type(arg) == float:

        return abs(arg)

    else:

        return "Nope"



'Python' 카테고리의 다른 글

Tkinter 를 이용한 시계  (0) 2017.06.27
Mac에서 Python 개발 환경 구축하기  (0) 2016.02.18
functions  (0) 2016.02.08
Pig Latin  (0) 2016.02.08
Conditional and control flow  (0) 2016.02.05

+ Recent posts