Skip to main content

Keywords in Python

Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier.


Here's a list of all keywords in Python Programming


Keywords in Python programming language

 >>> import keyword

 >>> print(keyword.kwlist)

 ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] 

Description of Keywords in Python with examples

True, False

True and False are truth values in Python. They are the results of comparison operations or logical (Boolean) operations in Python. For example.

>>> 1 == 1 

True

 >>> 5 > 3 

True

 >>> True or False

 True 

>>> 10 <= 1

 False

 >>> 3 > 7

 False

 >>> True and False 

False

None

None is a special constant in Python that represents the absence of a value or a null value.

It is an object of its own datatype, the None Type. We cannot create multiple None objects but can assign it to variables. These variables will be equal to one another.

We must take special care that None does not imply False, 0 or any empty list, dictionary, string etc. For example:

>>> None == 0

 False

 >>> None == [] 

False 

>>> None == False 

False

 >>> x = None

 >>> y = None

 >>> x == y 

True 

and, or , not

and, or, not are the logical operators in Python. and will result into True only if both the operands are True. 

>>> True and False 

False 

>>> True or False 

True 

>>> not False 

True

as

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:

>>> import math as myAlias >>>myAlias.cos(myAlias.pi) -1.0

Here we imported the math module by giving it the name my Alias. Now we can refer to the math module with this name. Using this name we calculated cos(pi) and got -1.0 as the answer.

assert

assert is used for debugging purposes.

While programming, sometimes we wish to know the internal state or check if our assumptions are true. assert helps us do this and find bugs more conveniently. assert is followed by a condition

>>> a = 4 

>>> assert a < 5 

>>> assert a > 5

File "<string>", line 301, in run code File "<interactive input>", line 1, in <module> Assertion Error: 

async, await

The async and await keywords are provided by the async library in Python. They are used to write concurrent code in Python. For example,

import async 

async def main(): 

                  print('Hello')

                  await async.sleep(1) 

                  print('world')




Comments

Popular posts from this blog

Python | Calendar Module

 Python | Calendar Module Python defines an inbuilt module calendar that handles operations related to the calendar. The calendar module allows output calendars like the program and provides additional useful functions related to the calendar. Functions and classes defined in the Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. By default, these calendars have Monday as the first day of the week, and Sunday as the last (the European convention).  Example #1: Display the Calendar of a given month.  # Python program to display calendar of # given month of the year  # import module import calendar yy = 2017 mm = 11  # display the calendar print(calendar.month(yy, mm))  Output:  Example #2: Display calendar of the given year.  # Python code to demonstrate the working of  # calendar() function to print calendar # importing calendar module  # for calendar operations import calendar  # using calendar to print calendar of year 

What Is Python Used For

 Python is an interpreted, object-oriented, high-level programming language with dynamic semantics developed by Guido van Rossum . It was originally released in 1991 . Designed to be easy as well as fun, the name "Python" is a nod to the British comedy group Monty Python. Python has a reputation as a beginner-friendly language, replacing Java as the most widely used introductory language because it handles much of the complexity for the user, allowing beginners to focus on fully grasping programming concepts rather than minute details. Python is used for server-side web development, software development, mathematics, and system scripting, and is popular for Rapid Application Development and as a scripting or glue language to tie existing components because of its high-level, built-in data structures, dynamic typing, and dynamic binding. Program maintenance costs are reduced with Python due to the easily learned syntax and emphasis on readability. Additionally, Python's s

Fourer series formula

 Fourer series formula