-
[Python tutorial] 26. Keyword코딩/Python 2022. 12. 21. 17:30728x90
and
A logical operator
x = (5 > 3 and 5 < 10)
as
To create an alias
import calendar as c
assert
For debugging
x = "hello" #if condition returns True, then nothing happens: assert x == "hello" #if condition returns False, AssertionError is raised: assert x == "goodbye"
break
To break out of a loop
for i in range(9): if i > 3: break
class
To define a class
class Person: name = "John" age = 36
continue
To continue to the
next iteration of a loopfor i in range(9): if i == 3: continue
def
To define a function
def my_function(): print("Hello from a function")
del
To delete an object
class MyClass: name = "John" del MyClass
elif
Used in conditional statements, same as else if
for i in range(-5, 5): if i > 0: print("YES") elif i == 0: print("WHATEVER") else: print("NO")
else
Used in conditional statements
x = 2 if x > 3: print("YES") else: print("NO")
except
Used with exceptions, what to do when an exception occurs
try: x > 3 except: print("Something went wrong")
False
Boolean value, result of comparison operations
print(5 > 6)
finally
Used with exceptions, a block of code that will be executed no matter if there is an exception or
nottry: x > 3 except: print("Something went wrong") else: print("Nothing went wrong") finally: print("The try...except block is finished")
for
To create a for loop
for x in range(1, 9):
from
To import specific parts of a module
from datetime import time x = time(hour=15)
global
To declare a global variable
#create a function: def myfunction(): global x x = "hello" #execute the function: myfunction() #x should now be global, and accessible in the global scope. print(x)
if
To make a conditional statement
x = 5 if x > 3: print("YES")
import
To import a module
import datetime
in
To check if a value is present in a list, tuple, etc.
fruits = ["apple", "banana", "cherry"] if "banana" in fruits: print("yes")
is
To test if two variables are equal
x = ["apple", "banana", "cherry"] y = x print(x is y)
lambda
To create an anonymous function
x = lambda a : a + 10 print(x(5))
None
Represents a null value
x = None
nonlocal
To declare a non-local variable
def myfunc1(): x = "John" def myfunc2(): nonlocal x x = "hello" myfunc2() return x print(myfunc1())
not
A logical operator
x = False print(not x)
or
A logical operator
x = (5 > 3 or 5 > 10)
pass
A null statement, a statement that will do nothing
for x in [0, 1, 2]: pass
raise
To raise an exception
x = -1 if x < 0: raise Exception("Sorry, no numbers below zero")
return
To exit a function and return a value
def myfunction(): return 3+3 print(myfunction())
True
Boolean value, result of comparison operations
print(7 > 6)
try
To make a try...except statement
try: x > 3 except: print("Something went wrong")
while
To create a while loop
x = 0 while x < 9: print(x) x = x + 1
with
Used to simplify exception handling
yield
To end a function, returns a generator
728x90'코딩 > Python' 카테고리의 다른 글
[Python tutorial] 27. Exception (0) 2022.12.21 [Python/Module] Datetime (1) 2022.12.21 [Python tutorial] 25. File Methods (0) 2022.12.21 [Python tutorial] 24. Set Methods (0) 2022.12.21 [Python tutorial] 23. Tuple Methods (0) 2022.12.21