Python 01.pptx

Python
https://www.programiz.com/python-programming/keywords-
identifier
Python
• Python is a cross-platform programming language, which means
that it can run on multiple platforms like Windows, macOS, Linux,
and has even been ported to the Java and .NET virtual machines.
It is free and open-source.
• Even though most of today's Linux and Mac have Python pre-
installed in it, the version might be out-of-date. So, it is always a
good idea to install the most current version.
The Easiest Way to Run Python
• The Thonny IDE comes with the latest version of Python bundled in it. So
you don't have to install Python separately.
• Follow the following steps to run Python on your computer.
1. Download Thonny IDE.
2. Run the installer to install Thonny on your computer.
• Go to: File > New. Then save the file with .py extension. For example,
hello.py, example.py, etc.
• You can give any name to the file. However, the file name should end
with .py
• Write Python code in the file and save it
Python 01.pptx
Thonny Python IDE
Features
• Easy to get started. Thonny comes with Python 3.10
built in, so just one simple installer is needed and
you're ready to learn programming. (You can also use a
separate Python installation, if necessary.) The initial
user interface is stripped of all features that may
distract beginners.
continue
• Simple debugger. Just press Ctrl+F5 instead
of F5 and you can run your programs step-by-
step, no breakpoints needed. Press F6 for a
big step and F7 for a small step. Steps follow
program structure, not just code lines.
• Step through expression evaluation. If you
use small steps, then you can even see how
Python evaluates your expressions. You can
think of this light-blue box as a piece of paper
where Python replaces subexpressions with
their values, piece-by-piece.
continue
• Highlights syntax errors. Unclosed quotes and
parentheses are the most common beginners' syntax
errors. Thonny's editor makes these easy to spot.
• Code completion. Students can explore APIs with the
help of code completion.
Python Keywords and Identifiers
• Keywords are predefined, reserved words used in Python
programming that have special meanings to the compiler.
• We cannot use a keyword as a variable name, function name, or
any other identifier. They are used to define the syntax and
structure of the Python language.
• All the keywords except True, False and None are in lowercase and
they must be written as they are. The list of all the keywords is
given below.
Python Keywords List
Rules for Naming an Identifier
• Identifiers cannot be a keyword.
• Identifiers are case-sensitive.
• It can have a sequence of letters and digits. However, it must
begin with a letter or _. The first letter of an identifier cannot be
a digit.
• It's a convention to start an identifier with a letter rather
• Whitespaces are not allowed.
• We cannot use special symbols like !, @, #, $, and so on.
Things to Remember
• Python is a case-sensitive language. This means, Variable and
variable are not the same.
• Multiple words can be separated using an underscore, like
this_is_a_long_variable.
Python Comments
Python Comments
• In computer programming, comments are hints that we use to
make our code more understandable.
• Comments are completely ignored by the interpreter. They are
meant for fellow programmers. For example,
Types of Comments in Python
• In Python, there are two types of comments:
• single-line comment
• multi-line comment
• Multi-line Comment in Python
• Python doesn't offer a separate way to write multiline comments.
• We can use # at the beginning of each line of comment on
multiple lines
Python Variables, Constants and
Literals
Python Variables
• In programming, a variable is a container (storage area) to hold
data. For example,
Changing the Value of a Variable in Python
Assigning multiple values to multiple variables
Python Constants
Python Literals
• Literals are representations of fixed values in a program. They can
be numbers, characters, or strings, etc. For example, 'Hello,
World!', 12, 23.0, 'C', etc.
Python Data Types
• In computer programming, data types specify the type of data
that can be stored inside a variable. For example,
Python List Data Type
• List is an ordered collection of similar or different types of items
separated by commas and enclosed within brackets [ ]. For
example,
• Here, we have created a list named languages with 3 string values
inside it.
Access List Items
• To access items from a list, we use the index number (0, 1, 2 ...).
For example,
Python Tuple Data Type
• Tuple is an ordered sequence of items same as a list. The only
difference is that tuples are immutable. Tuples once created
cannot be modified.
• In Python, we use the parentheses () to store items of a tuple. For
example,
Access Tuple Items
• Similar to lists, we use the index number to access tuple items in
Python . For example.
Python String Data Type
Python Set Data Type
• Set is an unordered collection of unique items. Set is defined by
values separated by commas inside braces { }. For example,
Python Type Conversion
• In programming, type conversion is the process of converting data
of one type to another. For example: converting integer data to
string.
• There are two types of type conversion in Python.
• Implicit Conversion - automatic type conversion
• Explicit Conversion - manual type conversion
Python Implicit Type Conversion
• Python automatically converts one data type to another. This is
known as implicit type conversion.
• Example 1: Converting integer to float
Explicit Type Conversion
• In Explicit Type Conversion, users convert the data type of an
object to required data type.
• We use the built-in functions like int(), float(), str(), etc to
perform explicit type conversion.
• This type of conversion is also called typecasting because the user
casts (changes) the data type of the objects.
Example 2: Addition of string and integer Using Explicit
Conversion
Key Points to Remember
• Type Conversion is the conversion of an object from one data type
to another data type.
• Implicit Type Conversion is automatically performed by the Python
interpreter.
• Python avoids the loss of data in Implicit Type Conversion.
• Explicit Type Conversion is also called Type Casting, the data types
of objects are converted using predefined functions by the user.
• In Type Casting, loss of data may occur as we enforce the object
to a specific data type.
Python Basic Input and Output
• Python Output
• In Python, we can simply use the print() function to print output.
For example,
Syntax of print()
• object - value(s) to be printed
• sep (optional) - allows us to separate multiple objects inside print().
• end (optional) - allows us to add add specific values like new line "n", tab
"t"
• file (optional) - where the values are printed. It's default value is
sys.stdout (screen)
• flush (optional) - boolean specifying if the output is flushed or buffered.
Default: False
Example 2: Python print() with end Parameter
Example 3: Python print() with sep parameter
Python input & output
• While programming, we might want to take the input from the
user. In python, we can use the input() function.
Example: Print Python Variables and Literals
Example: Print Concatenated Strings
Python Input
• While programming, we might want to take the input from the
user. In Python, we can use the input() function.
• Syntax of input()
Example: Python User Input
Python Operators
Types of Python Operators
1. Arithmetic operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Special Operators
1. Python Arithmetic Operators
Example 1: Arithmetic Operators in Python
2. Python Assignment Operators
• Assignment operators are used to assign values to variables. For
example,
Example 2: Assignment Operators
3. Python Comparison Operators
Example 3: Comparison Operators
4. Python Logical Operators
Python Namespace And Scope
What is Name in Python?
• Name (also called identifier) is simply a name given to objects.
Everything in Python is an object. Name is a way to access the
underlying object.
• For example, when we do the assignment a = 2, 2 is an object
stored in memory and a is the name we associate it with. We can
get the address (in RAM) of some object through the built-in
function id(). Let's look at how to use it.
Name in Python?
What is a Namespace in Python?
• To simply put it, a namespace is a collection of names.
• In Python, you can imagine a namespace as a mapping of every
name you have defined to corresponding objects.
• Different namespaces can co-exist at a given time but are
completely isolated.
• A namespace containing all the built-in names is created when we
start the Python interpreter and exists as long as the interpreter
runs.
• This is the reason that built-in functions like id(), print() etc. are
always available to us from any part of the program. Each module
creates its own global namespace.
Python 01.pptx
Python Variable Scope
• A scope is the portion of a program from where a namespace can
be accessed directly without any prefix.
• At any given moment, there are at least three nested scopes.
1. Scope of the current function which has local names
2. Scope of the module which has global names
3. Outermost scope which has built-in names
Python Variable Scope
• When a reference is made inside a function, the name is searched
in the local namespace, then in the global namespace and finally
in the built-in namespace.
• If there is a function inside another function, a new scope is
nested inside the local scope.
Example of Scope and Namespace in Python
• Here, the variable a is in the global namespace. Variable b is in
the local namespace of outer_function() and c is in the nested
local namespace of inner_function().
• When we are in inner_function(), c is local to us, b is nonlocal and
a is global. We can read as well as assign new values to c but can
only read b and a from inner_function().
Python 01.pptx
1 von 60

Recomendados

Python Programming 1.pptx von
Python Programming 1.pptxPython Programming 1.pptx
Python Programming 1.pptxFrancis Densil Raj
27 views258 Folien
Fundamentals of Python Programming von
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python ProgrammingKamal Acharya
9.8K views135 Folien
Python for katana von
Python for katanaPython for katana
Python for katanakedar nath
11.5K views37 Folien
Programming with Python: Week 1 von
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1Ahmet Bulut
442 views39 Folien
modul-python-part1.pptx von
modul-python-part1.pptxmodul-python-part1.pptx
modul-python-part1.pptxYusuf Ayuba
62 views23 Folien
Python intro von
Python introPython intro
Python introPiyush rai
509 views20 Folien

Más contenido relacionado

Similar a Python 01.pptx

intro to python.pptx von
intro to python.pptxintro to python.pptx
intro to python.pptxUpasnaSharma37
7 views17 Folien
Python Programming.pdf von
Python Programming.pdfPython Programming.pdf
Python Programming.pdfssuser9a6ca1
2 views13 Folien
Python Demo.pptx von
Python Demo.pptxPython Demo.pptx
Python Demo.pptxParveenShaik21
7 views85 Folien
Python Demo.pptx von
Python Demo.pptxPython Demo.pptx
Python Demo.pptxParveenShaik21
3 views85 Folien
Python (3).pdf von
Python (3).pdfPython (3).pdf
Python (3).pdfsamiwaris2
23 views30 Folien
Introduction to python von
Introduction to pythonIntroduction to python
Introduction to pythonAyshwarya Baburam
1.1K views62 Folien

Similar a Python 01.pptx(20)

prakash ppt (2).pdf von ShivamKS4
prakash ppt (2).pdfprakash ppt (2).pdf
prakash ppt (2).pdf
ShivamKS44 views
4_Introduction to Python Programming.pptx von Gnanesh12
4_Introduction to Python Programming.pptx4_Introduction to Python Programming.pptx
4_Introduction to Python Programming.pptx
Gnanesh1223 views
Full Python in 20 slides von rfojdar
Full Python in 20 slidesFull Python in 20 slides
Full Python in 20 slides
rfojdar703 views
INTRODUCTION TO PYTHON.pptx von Nimrahafzal1
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1120 views
Zero to Hero - Introduction to Python3 von Chariza Pladin
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3
Chariza Pladin7.6K views
Learn Python The Hard Way Presentation von Amira ElSharkawy
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
Amira ElSharkawy912 views

Último

BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports von
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsRa'Fat Al-Msie'deen
5 views49 Folien
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... von
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...Deltares
9 views24 Folien
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme... von
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...Deltares
5 views28 Folien
WebAssembly von
WebAssemblyWebAssembly
WebAssemblyJens Siebert
35 views18 Folien
A first look at MariaDB 11.x features and ideas on how to use them von
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use themFederico Razzoli
45 views36 Folien
Generic or specific? Making sensible software design decisions von
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
6 views60 Folien

Último(20)

BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports von Ra'Fat Al-Msie'deen
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... von Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 views
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme... von Deltares
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
Deltares5 views
A first look at MariaDB 11.x features and ideas on how to use them von Federico Razzoli
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use them
Federico Razzoli45 views
Generic or specific? Making sensible software design decisions von Bert Jan Schrijver
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h... von Deltares
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...
Deltares5 views
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... von TomHalpin9
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
TomHalpin95 views
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema von Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
Deltares17 views
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ... von Deltares
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
Deltares10 views
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... von Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
Deltares17 views
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx von animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm13 views
MariaDB stored procedures and why they should be improved von Federico Razzoli
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improved
Dapr Unleashed: Accelerating Microservice Development von Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
SUGCON ANZ Presentation V2.1 Final.pptx von Jack Spektor
SUGCON ANZ Presentation V2.1 Final.pptxSUGCON ANZ Presentation V2.1 Final.pptx
SUGCON ANZ Presentation V2.1 Final.pptx
Jack Spektor22 views
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko... von Deltares
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
Deltares12 views
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... von Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller38 views

Python 01.pptx

  • 2. Python • Python is a cross-platform programming language, which means that it can run on multiple platforms like Windows, macOS, Linux, and has even been ported to the Java and .NET virtual machines. It is free and open-source. • Even though most of today's Linux and Mac have Python pre- installed in it, the version might be out-of-date. So, it is always a good idea to install the most current version.
  • 3. The Easiest Way to Run Python • The Thonny IDE comes with the latest version of Python bundled in it. So you don't have to install Python separately. • Follow the following steps to run Python on your computer. 1. Download Thonny IDE. 2. Run the installer to install Thonny on your computer. • Go to: File > New. Then save the file with .py extension. For example, hello.py, example.py, etc. • You can give any name to the file. However, the file name should end with .py • Write Python code in the file and save it
  • 6. Features • Easy to get started. Thonny comes with Python 3.10 built in, so just one simple installer is needed and you're ready to learn programming. (You can also use a separate Python installation, if necessary.) The initial user interface is stripped of all features that may distract beginners.
  • 7. continue • Simple debugger. Just press Ctrl+F5 instead of F5 and you can run your programs step-by- step, no breakpoints needed. Press F6 for a big step and F7 for a small step. Steps follow program structure, not just code lines. • Step through expression evaluation. If you use small steps, then you can even see how Python evaluates your expressions. You can think of this light-blue box as a piece of paper where Python replaces subexpressions with their values, piece-by-piece.
  • 8. continue • Highlights syntax errors. Unclosed quotes and parentheses are the most common beginners' syntax errors. Thonny's editor makes these easy to spot. • Code completion. Students can explore APIs with the help of code completion.
  • 9. Python Keywords and Identifiers • Keywords are predefined, reserved words used in Python programming that have special meanings to the compiler. • We cannot use a keyword as a variable name, function name, or any other identifier. They are used to define the syntax and structure of the Python language. • All the keywords except True, False and None are in lowercase and they must be written as they are. The list of all the keywords is given below.
  • 11. Rules for Naming an Identifier • Identifiers cannot be a keyword. • Identifiers are case-sensitive. • It can have a sequence of letters and digits. However, it must begin with a letter or _. The first letter of an identifier cannot be a digit. • It's a convention to start an identifier with a letter rather • Whitespaces are not allowed. • We cannot use special symbols like !, @, #, $, and so on.
  • 12. Things to Remember • Python is a case-sensitive language. This means, Variable and variable are not the same. • Multiple words can be separated using an underscore, like this_is_a_long_variable.
  • 14. Python Comments • In computer programming, comments are hints that we use to make our code more understandable. • Comments are completely ignored by the interpreter. They are meant for fellow programmers. For example,
  • 15. Types of Comments in Python • In Python, there are two types of comments: • single-line comment • multi-line comment • Multi-line Comment in Python • Python doesn't offer a separate way to write multiline comments. • We can use # at the beginning of each line of comment on multiple lines
  • 17. Python Variables • In programming, a variable is a container (storage area) to hold data. For example,
  • 18. Changing the Value of a Variable in Python
  • 19. Assigning multiple values to multiple variables
  • 21. Python Literals • Literals are representations of fixed values in a program. They can be numbers, characters, or strings, etc. For example, 'Hello, World!', 12, 23.0, 'C', etc.
  • 22. Python Data Types • In computer programming, data types specify the type of data that can be stored inside a variable. For example,
  • 23. Python List Data Type • List is an ordered collection of similar or different types of items separated by commas and enclosed within brackets [ ]. For example, • Here, we have created a list named languages with 3 string values inside it.
  • 24. Access List Items • To access items from a list, we use the index number (0, 1, 2 ...). For example,
  • 25. Python Tuple Data Type • Tuple is an ordered sequence of items same as a list. The only difference is that tuples are immutable. Tuples once created cannot be modified. • In Python, we use the parentheses () to store items of a tuple. For example,
  • 26. Access Tuple Items • Similar to lists, we use the index number to access tuple items in Python . For example.
  • 28. Python Set Data Type • Set is an unordered collection of unique items. Set is defined by values separated by commas inside braces { }. For example,
  • 29. Python Type Conversion • In programming, type conversion is the process of converting data of one type to another. For example: converting integer data to string. • There are two types of type conversion in Python. • Implicit Conversion - automatic type conversion • Explicit Conversion - manual type conversion
  • 30. Python Implicit Type Conversion • Python automatically converts one data type to another. This is known as implicit type conversion. • Example 1: Converting integer to float
  • 31. Explicit Type Conversion • In Explicit Type Conversion, users convert the data type of an object to required data type. • We use the built-in functions like int(), float(), str(), etc to perform explicit type conversion. • This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.
  • 32. Example 2: Addition of string and integer Using Explicit Conversion
  • 33. Key Points to Remember • Type Conversion is the conversion of an object from one data type to another data type. • Implicit Type Conversion is automatically performed by the Python interpreter. • Python avoids the loss of data in Implicit Type Conversion. • Explicit Type Conversion is also called Type Casting, the data types of objects are converted using predefined functions by the user. • In Type Casting, loss of data may occur as we enforce the object to a specific data type.
  • 34. Python Basic Input and Output • Python Output • In Python, we can simply use the print() function to print output. For example,
  • 35. Syntax of print() • object - value(s) to be printed • sep (optional) - allows us to separate multiple objects inside print(). • end (optional) - allows us to add add specific values like new line "n", tab "t" • file (optional) - where the values are printed. It's default value is sys.stdout (screen) • flush (optional) - boolean specifying if the output is flushed or buffered. Default: False
  • 36. Example 2: Python print() with end Parameter
  • 37. Example 3: Python print() with sep parameter
  • 38. Python input & output • While programming, we might want to take the input from the user. In python, we can use the input() function.
  • 39. Example: Print Python Variables and Literals
  • 41. Python Input • While programming, we might want to take the input from the user. In Python, we can use the input() function. • Syntax of input()
  • 44. Types of Python Operators 1. Arithmetic operators 2. Assignment Operators 3. Comparison Operators 4. Logical Operators 5. Bitwise Operators 6. Special Operators
  • 45. 1. Python Arithmetic Operators
  • 46. Example 1: Arithmetic Operators in Python
  • 47. 2. Python Assignment Operators • Assignment operators are used to assign values to variables. For example,
  • 49. 3. Python Comparison Operators
  • 51. 4. Python Logical Operators
  • 53. What is Name in Python? • Name (also called identifier) is simply a name given to objects. Everything in Python is an object. Name is a way to access the underlying object. • For example, when we do the assignment a = 2, 2 is an object stored in memory and a is the name we associate it with. We can get the address (in RAM) of some object through the built-in function id(). Let's look at how to use it.
  • 55. What is a Namespace in Python? • To simply put it, a namespace is a collection of names. • In Python, you can imagine a namespace as a mapping of every name you have defined to corresponding objects. • Different namespaces can co-exist at a given time but are completely isolated. • A namespace containing all the built-in names is created when we start the Python interpreter and exists as long as the interpreter runs. • This is the reason that built-in functions like id(), print() etc. are always available to us from any part of the program. Each module creates its own global namespace.
  • 57. Python Variable Scope • A scope is the portion of a program from where a namespace can be accessed directly without any prefix. • At any given moment, there are at least three nested scopes. 1. Scope of the current function which has local names 2. Scope of the module which has global names 3. Outermost scope which has built-in names
  • 58. Python Variable Scope • When a reference is made inside a function, the name is searched in the local namespace, then in the global namespace and finally in the built-in namespace. • If there is a function inside another function, a new scope is nested inside the local scope.
  • 59. Example of Scope and Namespace in Python • Here, the variable a is in the global namespace. Variable b is in the local namespace of outer_function() and c is in the nested local namespace of inner_function(). • When we are in inner_function(), c is local to us, b is nonlocal and a is global. We can read as well as assign new values to c but can only read b and a from inner_function().