This tutorial has been written with the objective of teaching the basic concepts like keywords, variables and comments in Python programming language.This tutorial has been written with the objective of teaching the basic concepts like keywords, variables and comments in Python programming language.
Python has been one of the oldest and most used programming languages; it comes with many libraries and modules to develop many types of applications. Python is also very extensively used in machine learning, artificial intelligence and deep learning field.
Today we are going to learn keywords, variables, identifiers and comments in Python programming language. These topics are important basic concepts of Python and developer must learn these concepts in Python Programming Language. In Python keywords, identifiers, variables and comments are considered as building blocks of programming language. In this page we will see all these topics of Python with examples.
Like other programming languages Python also comes with the reserved keywords with special meaning and these can't be used to define variables, function and other objects in Python programming language. The reserved words are collectively known as Python keywords.
Here is the list of keywords in Python:
help> keywords Here is a list of the Python keywords. Enter any keyword to get more help. False class from or None continue global pass True def if raise and del import return as elif in try assert else is while async except lambda with await finally nonlocal yield break for not help>
These are current list of keywords in Python, but in future this list might change due to change in the Python programming language. So, you can check list of keywords in your Python distribution by tying help() on the python console, and on the help console type keywords as shown below:
Details of Python keywords:
print: The print is function in Python3 and it is used to print data on the console.
while: The while keyword is used for controlling the flow of the program on certain condition.
for: The for is used for looping in Python and it can also used for iterating the items in the collection.
break: The break keyword is used for coming out (interrupting) the execution of current loop.
continue: The continue keyword is used to interrupt current cycle and jump to to another place in the program. Here new cycle begins with the use of continue statement.
if: The if statement is Python is used for run the code conditionally if the condition is satisfied by the if statement.
elif: The elif stands for else if and it used with the if statement
else: The else statement is optional in Python and it is executed in case any of the if/elsif conditions are not satisfied
is: This is used to testing the object identity.
not: It is used to negates a boolean value
and: If 'and' statement is used then all the conditions in a boolean expression must be met
or: It is used for matching at least one condition out of
all the conditions.
import: The import statement is used to import and use other modules into a Python script.
as: This is used to give a module a different alias.
from: This is used for importing a specific variable, class or a function from a module in Pyton application.
def: The def keyword is used to create a new user defined function. You can use this keyword to make your own user defined function in Python program.
return: This keyword is used to exit the function and returns a value to the calling function.
lambda: The keyword is used to create a new anonymous function.
global: If use global keyword for a function then it will be accessible outside functions
try: This is use for exception handling in Python.
except: This keyword is used for catching the exception and executing codes
finally: This keyword is used with the try and catch block in Python, The finally block is always executed in the end. In this block resource cleanup is done.
raise: The raise is used to create a user defined exception in Python.
del: The del keyword is used to delete the object in Python program.
pass: does nothing
assert: This use for debugging code in Python program
class: The class keyword is used to create new user defined objects.
exec: The exec keyword is used to execute Python code dynamically.
yield: The yield keyword is used with generators
Identifiers in Python are the user-defined names used for defining variable, functions, modules or any other objects. It is the name given to objects in Python programming language. For example:
In the above code we are defining a variable with the identifier "name" and it is holding data "Deepak Kumar".
In Python there are strict rules of creating identifiers. Here are the rules of creating identifiers in Python programming language:
1. You can make identifiers with a sequence of letters (a-z or A-Z) and it can be mixed with digits or and underscore.
2. There must not be any space in the identifier name.
3. It must not start with digits but can end with a digit. For example 1name is invalid while name1 is valid identifier.
4. Reserved keywords can't be used as identifier.
For example following are invalid identifier declaration:
for = 10 is = 15 with = 10
Here is error message reported in Python:
In Python variables are the entity whose values can be changed in runtime. Variables are used to store values which can be used later point of time in the code during execution of program. Following is simple example of using the variables in Python:
i=10 j=20 k = i+j print(k)
Variables are label given to in-memory object and it only exists during the execution of program. You can use variables to store data in the memory during the program execution. For example in the above code we have declared two variables i and j, then added these two variables and stored the value in k variable. Finally printed the value of k on the console.
Code commenting is very important in programming and it is must to properly comment your code. Code commenting is considered as one of the best practices in software development.
There are two types of comments in Python:
a) Single line comment
The single line comment is for commenting one line of come and # sign is used to comment one line. Here is example:
# this is one line comment
b) Multiline comment
The multiline comment is used to comment more than one line. It is used to write comments in paragraph and at a time you can comment many lines
Here is example of multiline comment:
''' This is multi line comment in python This program is for calculating the sum of two numbers and then print the output on pyton console ''' i=10 j=20 k = i+j print(k)
In this tutorial we have learned about the Python keywords, variables, identifiers and comments.