IT

Python / Progate

호랑구야 2020. 7. 17. 09:00

 

python

 

- print string

print('Hello world')

print("Hello world")

 

- comment

# ~A~

 

- difference between Strings and Integers

print(1+2) -> 3

print('1+2') -> 1+2

 

- varible

varible_name = value

name = '~A~'

number = 0

 

print(name)

-> ~A~

print('number')

-> number

 

-update value of a variable

x = 1

 

: overwrite

x = 2

 

:calculation

x = x+2 (standard)

x += 2 (shorthand)

//

% 나머지

/ 나누기

 

- String Concatenation

print('number' + x)

-> number 6

 

- Type Conversion

str()

: string data type

int()

: integer data type

 

- if statements

if x==6 :

print('x is 6')

elif x==5 :

print('x is 5')

else :

print('x is neither 6 nor 5')

 

 

- Conditional Expressions

==

:equal

 

!=

: not equal

 

 

- Booleans

: data type has only two values, True and False.

 

 

- Conditional Operators

<

: less than

 

<=

: less than or equal to

 

>

: bigger than

 

>=

: bigger than or equal to

 

- Combining Conditions

and

: both con1 and con2 are true

if con1 and con2

 

or

: con2 or con2 is true

if con1 or con2

 

not

: con is not true

if not con==true

 

 

 

- getting input

input()

input_number = input('What is your number? ')

-> What is your number? (this is your answer)

# this input data type is always "string". you need to convert to an inter sometimes.

 

 

-lists

list_name = [value, ... ]

number = ['zero', 'one', 'two']

print(number[0])

->zero

 

number[0] = 'three'

print(number[0])

->three

 

number.append('four')

print(number)

->['three', 'one', 'two', 'four']

 

 

- Dictionaries

variable_name = {key : value, ... }

 

number = {'zero' : 0, 'one' : 1, 'two' : 2}

# dictionary don't have a fixed order

 

print(number['three'])

-> 3

 

- modify

number['zero'] = 3

 

-add

number['four'] = 4

 

 

-for loops

number = ['zero', 'one', 'two']

for num in number:

print('My number is ' + num)

 

number = {'zero' : 0, 'one' : 1, 'two' : 2}

for num in number:

print(num + ' is ' + str(number[num]))

 

 

 

- while loops

 

x = 5

s

while x<=10 :

print(x)

x += 1

 

number = [0, 1, 2, 3, 4]

for num in number:

print(num)

if num == 3:

break

 

for num in number:

if num % 2 == 0 :

continue -> skip the code under when condition is true

print(num)

 

 

- functions

 

def function_name(parameter, ...):

~A~

return value

 

function_name(argument, ...)

 

 

- Modules

 

import module_name

 

module_name,function_name()

 

 

- Python Standard Library

math random datetime

random.randini(x, y)

 

 

utils.py

 

def validate(number) :

if number<0 or number>5 :

return False

retrun True

 

def call_the_number(name = 'Who', num) :

num = [0, 1, 2, 3, 4, 5]

print('Ms. ' + name + ''s number is ' + num)

 

def judge(customer, master) :

if customer == master :

return 'Match the point!'

else :

return 'No, you number is not same as mine.'

 

 

 

import utils

import random

 

def ask_name() :

print('You can choose your number!')

customer_name = input('Can I ask your name?: ')

if customer_name =='Customer' :

return : Who are you?

return 'Ms. ' + name + ', welcome!'

 

def ask_number() :

print('choose your number --> 0~5 : ')

customer_number = int(input('What is your number?: '))s

 

def result() :

if utils.validate(player_number) == True :

computer_number = random.randini(0, 5)

utils.call_the_number(customer_number, customer_name)

utils.call_the_number(compter_number, 'Master')

 

result = utils.judge(customer_number, compter_number)

 

print(result)

else :

print('That is not valid number, choose again please')

ask_name()

 

 

while :

 

ask_name()

ask_number()

result()

 

 

 

 

- class

 

1. prepare a Class

 

 -> Always start with a capital letter

class ClassName :

pass

  -> shows that there is no code to run

 

2. Create an Instance of the Class

3. Add Data to the Instance

 

instance = ClassName()

instance.instance_variable = ~A~

print('instance.instance_variable_name')

 

option_stuff1 = OptionStuff()

option_stuff1.title = ~A~

print('option_stuff.title')

 

-> ~A~

 

 

- __init__

: thing that combining step2 and 3.

 

 

class OptionStuff :

   -> always put 'self' for the 1st argument

def greet(self) :

print('How are you doing?')

 

 

option_stuff1 = OptionStuff()

option_stuff1.greet()        -> call the method defined in the class

 

# or use the return in the function(instance methods).

 

class OptionStuff :

def greet(self) :

return 'How are you doing?'

 

option_stuff1 = OptionStuff()

print(option_stuff1.greet())

 

 

class OptionStuff :

def __init__(self) :

self.greet = 'How are you doing?'

 

option_stuff1 = OptionStuff()

print(option_stuff1.greet())

 

 

class OptionStuff :

def __init__(self, greet) : greet

self.greet =

 

option_stuff1 = OptionStuff('How are you doing?')

print(option_stuff1.greet)

 

 

 

- import classes

 

form Module_name import Class_name

 

 

 

- inheritance

class childClassName(parentClassName) :

 

class Sing(OptionStuff):

pass

 

 

반응형