conditional statement and loop statement in Python conditional statement (if statement) One Condition Keyword: if if [condition] : [space]code you want to learn
Multiple Condition Keyword: elif if must there before use elif if [condition] : [space]code you want to learn elif [condition] : [space]code you want to learn elif [condition] : [space]code you want to learn Other than Condition Keyword: else if must there before use else if [condition] : [space]code you want to learn else : [space]code you want to learn if Statement Example a = 3 if a > 5: print("a is bigger than 5") elif a > 0: print("a is bigger than 0 but smaller than 5") else: print("a is negative") Loop Statement (for Statement) Repeat Example 10 times loop for i in range(10): print("Hi") list loop Example Repeat as much as elements in list a = ["A", "B", "C"] for i in a: print(i) # A B C list loop with index Example Repeat as much as elements in list for i, j in enumerate(a): print(i, ":", j) # 0 : A # 1 : B # 2 : C add two list and loop Example The pair’s data type is tuple numbers = [9, 7, 7] letters = ["z", "x", "y"] for pair in zip(numbers, letters): print(pair) # (9, 'z') # (7, 'x') # (7, 'y') Loop with certain index Example 5 to 9 for i in range(5, 10): # 5 ~ 9 print("Hello", i) # Hello 5 # Hello 6 # Hello 7 # Hello 8 # Hello 9 loop until certain condition loop until i is “o” a = "balloon" for i in a: print(i) if i == "o": break # b a l l o Loop Statement (while Statement) loop until certain condition loop until i is bigger than 5 i = 0 while i < 5: print(i) i = i + 1 # 0 1 2 3 4
Set Definition One of the data structure in python. Create set of data Data Structures in Python List: Tuple: ( ) Dictionary: { key : value } set: { } declaration use { and } to declare setA={1,3,4,"test"} # {1, 'test', 3, 4} setB={2,4,"test",5,6} # {2, 'test', 4, 5, 6} Characteristics No duplicated value Do not care about order set algebra(union, intersection, difference, symmetric difference) Set Algebra union Keyword: “|”, “union()”
Dictionary Definition One of the data structure in python. has key and value bined. You can fine value when you know the key very slimier with hashmap Data Structures in Python List Tuple Dictionary set declaration use { and } to declare the value can be list the key can be int or string. But not list. (TypeError: unhashable type: ‘list’) dictionary={"key1":"value1","key2":"value2","key3":"value3"} how to use indexing like List get() method difference between the indexing and get method If key does not exist, the indexing will return error.
get Coordinate by image pyautogui started left-top side of monster. If there are multiple images on screen, only return the coordinate of most left-top side image. If there is no same image, return FileNotFoundError
import pyautogui def getCoordinateByImg(imgAddress): imgLocatedCoordinate = pyautogui.locateOnScreen(imgAddress) # get image from the address if (imgLocatedCoordinate): return pyautogui.center(imgLocatedCoordinate) # return the center coordinate. else: print("image never found") raise FileNotFoundError 에러 pyscreeze.PyScreezeException: The Pillow package is required to use this function: type “pip install Pillow –upgrade” in terminal
how to install pip install pyautogui
Finding Coordinate It will display current mouse coordinate every second.
import pyautogui import time while True: print("Current Mouse Position : ", pyautogui.position()) time.sleep(1) mouse control import pyautogui pyautogui.moveTo(300, 300) # move (x=300, y=300) pyautogui.moveRel(x, y, t) # move (from current pos, move x and y with time. If t is 2, it will take 2 second to move) pyautogui.click() # click pyautogui.doubleClick() # double click pyautogui.
Tuple Definition One of the data structure in python. Almost same as List But Tuple is not editable. Object. Data Structures in Python List Tuple Dictionary set declaration use “(” and “)” to declear. But, if there is a element in Tuple, you need to add comma
tuple1=(1,) tuple2=(1,2,3) notTuple=(0) #wrong, this is int type. comma needed index structure same as List
characteristic Tuple is not editable.
List Definition One of the data structure in python. Almost same as array in other languages. Object. Data Structures in Python List Tuple Dictionary set declaration temp=[] temp=list() temp=[5,9,44] index structure characteristic List in List is possible. This is same as 2D array in other languages String in python is same as List
slicing [starting index : ending index-1 : condition index]
[0:8:3] = select index 0 to 7 by adding 3 [:8:] = select index 0 to 7 by adding 1 [7::] = select index 7 to -1 by adding 1 [::-1] = select index 0 to -1 by adding -1 a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(a[0:8:3]) # [0, 3, 6] print(a[:8:]) # [0, 1, 2, 3, 4, 5, 6, 7] print(a[7::]) # [7, 8, 9] print(a[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Arithmetic Operation a=[1,2,3] b=["apple","house","computer"] print(a+b) # [1, 2, 3, 'apple', 'house', 'computer'] # print(a+3) # error # print(a-b) # error # print(a-3) # error # print(a*b) # error print(b*3) # ['apple', 'house', 'computer', 'apple', 'house', 'computer', 'apple', 'house', 'computer'] print(b*0) # [] # print(a/b) # error # print(a/3) # error add element append() = add a new element behind extend() = add new elements behind insert() = add new elements by index [] = add new elements by index condition a=[1,2,3,4,5,6,7,8,9] b=["apple","house","computer"] a.
kinds of Arithmetic Operation in Python Symbol explaination return + Addition depend on input type - Subtraction depend on input type * Multiplication depend on input type / Division real number // Division Integer % Remaineder real number ** power of depend on input type and and logic gare T or F or or logic gate T or F < less than T or F > greater than T or F <= less than or equal T or F >= greater than or equal T or F == equal T or F Result +, -, *, /, //, %, **, <, >, <=, >= table A B result int int int A+B result int float float A+B result int bool-T int A+1 result int bool-F int A+0 result int none TypeError int String TypeError float bool-T float A+1 result float bool-F float A+0 result float none TypeError float String TypeError bool none TypeError bool String TypeError none String TypeError When computer created and programming language started, 1 was true and 0 was false.
Kinds of Variables scalar and non-scalar both object
scalar int : integer float : real number bool : True or False none : Null non-scalar String : data values that are made up of ordered sequences of characters, such as “hello world” checking data type print(type(Variable)) Case-Sensitive a and A are different variable
Casting x = str(3) # "4" y = int(3) # 4 z = float(3) # 4.