Python reserved these words for keywords.
Python has 33 keywords.
All the keywords except True, False and None must be written in lowercase.
Below is all the keywords.
- True, Flase : Truth values in Python. They are used for result of operations.
- class : class is used to define a class.
- is: This is used for testing object identify. is test if the two variables refere to a same object.
- return : This keywords is used inside a function to return a value. If you don't return any value, None is returned automatically.
- None : This is a constant that represents an absence of a value or null. You have to notice(You should be aware) that None is not equal to False, 0, or any empty list etc. Also(Also,) void functions will automatically return a None object automatically.
- continue, break : These keywords are used with looping. break ends looping. continue skip over current iteration of the loop.
- for : for is used for looping. This statement is similar to For Each statement of VB.net. The format is shown below :
for item in sequence:
Each elemtent of sequence is stored in item. Also, sequence needs to be of an
iterable type. (need to be of A = Aである必要がある)
str = "Nora Tyng"
for c in str:
The output is shown below:
- lambda : You can create a function without name. The function doesn't contain a return keyword, but the function contains an expression which is evaluated and returned.
The format is shown below :
lambda args: expression
You can specify more than one argment.
- except, raise, try,finally : These are used with exceptions. try...except blocks are used to catch exceptions. raise keyword is used to raise an exception explicitly.
finally is used with try...except block. finally block is executed absolutely. So you
can code for cleaning up.
- def : def is used to define a function. The format is shown below:
name is name of the function, parameters is used in the function.
- nonlocal : This keyword is very similar to global keyword. But nonlocal functions inside nested functions.
- while : while is used for looping. while loop continue until a condition for the loop is evaluated to False. Also 0 is equal to False.
- and, or, not : They are the logical operators. and returns True if both operands are true. or returns True if at least one operand is True. not returns True if a operand is False. if the operands is True, not returns False.
- del : You can delete a reference to an object. Also you can delete items from a list or a dictionary. It's useful!
- global : This is used to declare that a variable inside a function is global. If you want to modifiy the value of the global variable inside the function, you must use this keyword. But if you just read the value, you don't need this keyword.
- with : with statement executes initializing method and finalizing method automatically. So if you use this statement, an object is needed to implement __enter__ and __exit__ methods. You can code simply with with statement I think.
- as : as is used to give a another name to a module.
- if, elif, else : You can use these keywords for conditional branching in if...else statement.
if expression :
elif expression :
expression is a condition. elif is used for multi conditional branching. Also, when
any conditions are false, statements are executed in else block if you have the
- yield : Think about a situation that when you create a function that reads a data neede big memory. You can use return keyword to return the data. But a variable for the result is needed big size. You can choose yield keyword instead of return keyword. In a nutshell yield keyword is subdived return.
- assert : assert is used for checking an internal state. You code a condition after this keyword. If the condition is true, nothing happens. But if the conditions are false, AssertionError is outputed. Also, you can specify an error message after the condition.
- import,from : This is used to import modules. Also you can use from...import to import a specific attribute or function.
- pass : When this statement is executed, nothing happens. So you can use it as a placeholder.
- in : You can test if a sequence contains a value.