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
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.
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
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().