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 cale...

Fourer series formula

 Fourer series formula

DABASE MANAGEMENT SYSTEM QUESTIONS

 DABASE MANAGEMENT  SYSTEM SHORT QUESTIONS AND ANSWERS 1. What is database? A database is a logically coherent collection of data with some inherent meaning, representing some aspect of real world  and which is designed, built and populated with data for a specific purpose. 2. What is DBMS? It is a collection of programs that enables user to create and maintain a database. In other words it is general-purpose  software that provides the users with the processes of defining, constructing and manipulating the database for various  applications. 3. What is a Database system? The database and DBMS software together is called as Database system. 4. What are the advantages of DBMS? 1. Redundancy is controlled.  2. Unauthorised access is restricted.  3. Providing multiple user interfaces.  4. Enforcing integrity constraints.  5. Providing backup and recovery.  5. What are the disadvantage in File Processing System? 1. Data redundancy and incons...