Properties Of Python
- High level language
- Interpreted language
- Open source and community development
- Dynamically typed
- Object Oriented language
Keywords
- and, or, is, in, not
- break, continue, return, yield
- class, def, lambda, with
- if, elif, else, for, while, pass
- try, except, raise, finally, assert
- True, False, None
- from, import, as
- global, nonlocal, del
Can be printed using
import keyword
print(keyword.kwlist)
Namespace and Scope
Types of Namespaces: When Python interpreter
runs solely without any user-defined modules, methods, classes, etc.
Some functions like print(), id() are always present, these are built-in
namespaces. When a user creates a module, a global namespace gets
created, later the creation of local functions creates the local
namespace. The built-in namespace encompasses the global namespace and
the global namespace encompasses the local namespace.
A lifetime of a namespace depends upon the scope of objects, if the scope of an object ends, the lifetime of that namespace comes to an end.
But in some cases, one might be interested in updating or processing global variables only. That can be done using global and nonlocal keywords
Scope refers to the coding region from which a particular Python object is accessible. Hence one cannot access any particular object from anywhere from the code, the accessing has to be allowed by the scope of the object.
Rules of global keyword
A lifetime of a namespace depends upon the scope of objects, if the scope of an object ends, the lifetime of that namespace comes to an end.
But in some cases, one might be interested in updating or processing global variables only. That can be done using global and nonlocal keywords
Scope refers to the coding region from which a particular Python object is accessible. Hence one cannot access any particular object from anywhere from the code, the accessing has to be allowed by the scope of the object.
Rules of global keyword
- If a variable is assigned a value anywhere within the function's body, it's assumed to be a local unless explicitly declared as global.
- Variables that are only referenced inside a function are implicitly global.
- We Use global keyword to use a global variable inside a function.
- There is no need to use global keyword outside a function.
Statements, Indentation and Comments
Multi-Line Statements: Statements in Python
can be extended to one or more lines using parentheses
(), braces {},
square brackets [], semi-colon
;, continuation character slash
/.
Comments
# Declared using Continuation Character (\)
s = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9
# Declared using parentheses ()
n = (1 * 2 * 3 + 7 + 8 + 9)
# Declared using square brackets []
footballer = ['MESSI',
'NEYMAR',
'SUAREZ']
# Declared using braces {}
x = {1 + 2 + 3 + 4 + 5 + 6 +
7 + 8 + 9}
# Declared using semicolons(;)
flag = 2; ropes = 3; pole = 4
A block is a combination of all these
statements. Block can be regarded as the grouping of statements for a
specific purpose. Most of the programming languages like C, C++, Java
use braces { } to define a block of code. Python uses indentation
instead. Comments
- Single line using #
- Multi line using triple quotes, either single ''' or double """