0% found this document useful (0 votes)
6 views4 pages

Python Keywords and Identifiers Guide

Uploaded by

sagar sarkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Python Keywords and Identifiers Guide

Uploaded by

sagar sarkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Keywords

Keywords are the reserved words in python

We can't use a keyword as variable name, function name or any other identifier

Keywords are case sentive

In [1]:
#Get all keywords in python 3.6

import keyword

#print([Link])
"Total number of keywords ", [Link]

Out [1]: ('Total number of keywords ',


['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'])

In [4]:
"Total number of keywords ", len([Link])

Out [4]: ('Total number of keywords ', 35)

Identifiers

Identifier is the name given to entities like class, functions, variables etc. in Python. It helps
differentiating one entity from another.

Rules for Writing Identifiers:

1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0


to 9) or an underscore (_).

2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.

3. Keywords cannot be used as identifiers.

In [2]:
global = 1

File "<ipython-input-2-3d177345d6e4>", line 1


global = 1
^
SyntaxError: invalid syntax

We cannot use special symbols like !, @, #, $, % etc. in our identifier.

a@ = 10 #can't use special symbols as an identifier

Python Comments

Comments are lines that exist in computer programs that are ignored by compilers and interpreters.

Including comments in programs makes code more readable for humans as it provides some
information or explanation about what each part of a program is doing.

In general, it is a good idea to write comments while you are writing or updating a program as it is easy
to forget your thought process later on, and comments written later may be less useful in the long
term.

In Python, we use the hash (#) symbol to start writing a comment.

In [3]:
#Print Hello, world to console
print("Hello, world")

Hello, world

Multi Line Comments

If we have comments that extend multiple lines, one way of doing it is to use hash (#) in the beginning
of each line.

In [6]:
#This is a long comment
#and it extends
#Multiple lines
Another way of doing this is to use triple quotes, either ''' or """.

In [7]: """This is also a


perfect example of
multi-line comments"""

Out [7]: 'This is also a\nperfect example of\nmulti-line comments'

DocString in python

Docstring is short for documentation string.

It is a string that occurs as the first statement in a module, function, class, or method definition.

In [9]:
def double(num):
"""
function to double the number
"""
return 2 * num

print (double(10))

20

In [10]:
print(double.__doc__) #Docstring is available to us as the attribute __doc_

function to double the number

Python Indentation

1. Most of the programming languages like C, C++, Java use braces { } to define a block of code.
Python uses indentation.

2. A code block (body of a function, loop etc.) starts with indentation and ends with the first
unindented line. The amount of indentation is up to you, but it must be consistent throughout
that block.

3. Generally four whitespaces are used for indentation and is preferred over tabs.

In [12]:
for i in range(10):
print (i)

0
1
2
3
4
5
6
7
8
9

Indentation can be ignored in line continuation. But it's a good idea to always indent. It makes the code
more readable.

In [14]:
if True:
print("Machine Learning")
c = "AAIC"

Machine Learning

In [15]:
if True: print()"Machine Learning"; c = "AAIC"

Machine Learning

Python Statement

Common questions

Powered by AI

Using four spaces for indentation is recommended for consistency and readability in Python. While technically any indentation amount can be used, Python's style guidelines suggest four spaces as they visually align code blocks neatly and avoid the confusion or errors that can arise from mixing tabs with spaces, which may display differently across various text editors and tools .

Python supports several styles for declaring strings, including single quotes (' '), double quotes (" "), and triple quotes (''' or """). Single and double quotes are commonly used for single-line strings, while triple quotes are used for strings that span multiple lines. Docstrings, which provide inline documentation, also utilize triple quotes, allowing descriptions to be written in larger blocks .

In Python, multi-line comments can be created by placing a hash (#) at the beginning of each line intended to be commented out. Alternatively, multi-line comments can also be contained within triple quotes (''' or """). Triple quotes are more commonly used for writing docstrings, which provide a formal documentation mechanism in Python .

In Python, indentation indicates a block of code, such as the body of loops, functions, or conditionals. Unlike languages such as C++ or Java, which use braces to define code blocks, Python relies strictly on indentation levels. This means the start of a code block is defined by a consistent level of indentation, and the block ends when the indentation decreases. Generally, four whitespaces are used as the indentation level .

Python allows line continuation to break long lines into multiple parts to enhance readability. This can be achieved using a backslash (\) at the end of a line. While indentation can technically be ignored in line continuation, maintaining a consistent indentation level is recommended for improved readability and structured code appearance .

Python's use of whitespace for defining code blocks encourages a clean and consistent coding style, as programmers must maintain uniform indentation. This can reduce instances of syntax errors related to misplaced braces but increases the chance of errors related to inconsistent indentation. However, it often results in more readable code and encourages good programming habits .

Docstrings in Python are documentation strings that describe what a module, function, class, or method does. They are written as the first statement in the definition and are marked by triple quotes (''' or """). Docstrings can be accessed using the .__doc__ attribute of the respective Python object, providing a description of its functionality .

A valid Python identifier can consist of letters (a-z, A-Z), digits (0-9), and underscores (_), but it cannot start with a digit. It also cannot include symbols like !, @, #, $, % etc. Keywords cannot be used as identifiers because they are reserved words in Python that have special meaning and functionality within the language, which would conflict with user-defined names .

The 'global' keyword in Python is reserved and is used to declare the global scope of a variable inside a function. Attempting to use 'global' as an identifier results in a SyntaxError because it conflicts with the language's reserved vocabulary and thus cannot be redefined as a variable name. This is evident when tried, Python throws a syntax error .

Python uses the hash (#) symbol to indicate single-line comments. For multi-line comments, there are two approaches: writers can either use a hash symbol at the start of every line or employ triple quotes (''' or """). The latter is typically used for docstrings, which are more formally recognized documentation strings in functions and classes .

You might also like