python keywords

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

break

Used inside of for and while loops to alter their normal behaviour. break will end the smallest loop it is in and control flows to the statement immediately below the loop.

and

and will result into True only if both the operands are True.

True, False

are truth values in Python. They are the results of comparison operations or logical (Boolean) operations in Python.

except, raise, try

are used with exceptions in Python. Exceptions are basically errors that suggests something went wrong while executing our program. IOError, ValueError, ZeroDivisionError, ImportError, NameError, TypeError etc. are few examples of exception in Python. try...except blocks are used to catch exceptions in Python. We can raise an exception explicitly with the raise keyword.

class

class is used to define a new user-defined class in Python. Class is a collection of related attributes and methods that try to represent a real world situation. This idea of putting data and functions together in a class is central to the concept of object-oriented programming (OOP). Classes can be defined anywhere in a program. But it is a good practice to define a single class in a module.

def

def is used to define a user-defined function. Function is a block of related statements, which together does some specific task. It helps us organize code into manageable chunks and also to do some repetitive task.

gobal

is used to declare that a variable inside the function is global (outside the function). If we need to read the value of a global variable, it is not necessary to define it as global. This is understood. But if we need to modify the value of a global variable inside a function, then we must declare it with global. Otherwise a local variable with that name is created.

not

operator is used to invert the truth value.

pass

pass is a null statement in Python. Nothing happens when it is executed. It is used as a placeholder.

or

will result into True if any of the operands is True.

continue

Used inside of for and while loops to alter their normal behaviour. continue causes to end the current iteration of the loop, but not the whole loop.

assert

assert is used for debugging purposes to check if our assumptions are true.assert helps us do this and find bugs more conveniently. assert is followed by a condition. If the condition is true, nothing happens. But if the condition is false, AssertionError is raised.

del

del is used to delete the reference to an object. Everything is object in Python. We can delete a variable reference using del

if, else, elif

if, else, elif are used for conditional branching or decision making. When we want to test some condition and execute a block only if the condition is true, then we use if and elif. elif is short for else if. else is the block which is executed if the condition is false.

is

is is used in Python for testing object identity. While the == operator is used to test if two variables are equal or not, is is used to test if the two variables refer to the same object. It returns True if the objects are identical and False if not.

while

is used for looping in Python. The statements inside a while loop continue to execute until the condition for the while loop evaluates to False or a break statement is encountered.

for

is used for looping. Generally we use for when we know the number of times we want to loop. In Python we can use it with any type of sequence like a list or a string.

yield

is used inside a function like a return statement. But yield returns a generator. Generator is an iterator that generates one item at a time. A large list of value will take up a lot of memory. Generators are useful in this situation as it generates only one value at a time instead of storing all the values in memory.

as

is used to create an alias while importing a module. It means giving a different name (user-defined) to a module while importing it. As for example, Python has a standard module called math. Suppose we want to calculate what cosine pi is using an alias. We can do it as follows using as:

in

is used to test if a sequence (list, tuple, string etc.) contains a value. It returns True if the value is present, else it returns False.

finally

is used with try...except block to close up resources or file streams. Using finally ensures that the block of code inside it gets executed even if there is an unhandled exception.

from

keyword is used to import modules into the current namespace. from...import is used to import specific attributes or functions into the current namespace.

lambda

lambda is used to create an anonymous function (function with no name). It is an inline function that does not contain a return statement. It consists of an expression that is evaluated and returned.

return

statement is used inside a function to exit it and return a value. If we do not return a value explicitly, None is returned automatically.

with

statement is used to wrap the execution of a block of code within methods defined by the context manager. Context manager is a class that implements __enter__ and __exit__ methods. Use of with statement ensures that the __exit__ method is called at the end of the nested block. This concept is similar to the use of try...finally block.


Set pelajaran terkait

Sociology - Real World - Ch 8: Race / Ethnic Group Issues

View Set

Chapter 25: The Child with Gastrointestinal Dysfunction

View Set

CHAPTER 8 LESSON 4: DIETARY FAT RECOMMENDATIONS

View Set