Unit1pptx__2022_07_19_10_39_53 (1)
Unit1pptx__2022_07_19_10_39_53 (1)
Datatypes, Operators
Installation and working with python
• Download the Anaconda installer.
• https://www.anaconda.com/download/#windows
• RECOMMENDED:
• Verify data integrity with SHA-256.
• https://docs.anaconda.com/anaconda/install/hashes/
• For more information on hashes, see
• https://conda.io/projects/conda/en/latest/user-guide/install/download.html#cryptographic-
hash-verification
• Double-click the installer to launch.
Installation and working with python
Installation and working with python
• Choose whether to register
Anaconda as your default
Python. Unless you plan on
installing and running
multiple versions of
Anaconda or multiple
versions of Python, accept
the default and leave this box
checked.
• Click Install. If you want to
watch the packages
Anaconda is installing, click
Show Details.
Installation and working with python
Installation and working with python
Verifying your installation
Windows: Click
Start, search or
select Anaconda
Navigator from the
menu.
Verifying your installation
• If you prefer using a
command line interface
(CLI), you can use conda
to verify the installation
using Anaconda Prompt
• To open Anaconda
Prompt:
• Windows: Click Start,
search, or select
Anaconda Prompt
from the menu.
Verifying your installation
• After opening Anaconda Prompt or the terminal, choose any of the
following methods to verify:
• Enter command ‘conda list’
• If Anaconda is installed and working, this will display a list of installed
packages and their versions.
• Enter the command ‘python’
• This command runs the Python shell.
• If Anaconda is installed and working, the version information it displays when it
starts up will include “Anaconda”.
• To exit the Python shell, enter the command ‘quit()’
• Open Anaconda Navigator with the command ‘anaconda-navigator’
• If Anaconda is installed properly, Anaconda Navigator will open.
Verifying your installation
Verifying your installation
Features of Python
• Easy to Read, Learn and Write
• Python is a high-level programming language that has English-like syntax. This
makes it easier to read and understand the code.
• You need less lines of code to perform the same task as compared to other major
languages like C/C++ and Java.
• Improved Productivity
• Due to the simplicity of Python, developers can focus on solving the problem.
• They don’t need to spend too much time in understanding the syntax or behavior of
the programming language.
• Interpreted Language
• Python is an interpreted language which means that Python directly executes the code
line by line.
• In case of any error, it stops further execution and reports back the error which has
occurred.
• Python shows only one error even if the program has multiple errors. This makes
debugging easier.
Features of Python
• Dynamically Typed
• Python doesn’t know the type of variable until we run the code.
• It automatically assigns the data type during execution.
• The programmer doesn’t need to worry about declaring variables and their data types.
• Free and Open-Source
• Python comes under the OSI approved open-source license. This makes it free to use
and distribute.
• You can download the source code, modify it and even distribute your version of
Python.
• This is useful for organizations that want to modify some specific behavior and use
their version for development.
• Vast Libraries Support
• The standard library of Python is huge, you can find almost all the functions needed
for your task. So, you don’t have to depend on external libraries.
Features of Python
• Portability
• In many languages like C/C++, you need to change your code to run the program on
different platforms.
• That is not the same with Python. You only write once and run it anywhere.
• Support for GUI
• GUI or Graphical User Interface is one of the key aspects of any programming
language because it has the ability to add flair to code and make the results more
visual.
• Python has support for a wide array of GUIs which can easily be imported to the
interpreter, thus making this one of the most favorite languages for developers.
• Object-Oriented Approach
• One of the key aspects of Python is its object-oriented approach.
• This basically means that Python recognizes the concept of class and object
encapsulation thus allowing programs to be efficient in the long run.
• High-Level Language
• Python has been designed to be a high-level programming language, which means
that when you code in Python you don’t need to be aware of the coding structure,
architecture as well as memory management.
Features of Python
Python Interpreter and its working
Python Interpreter and its working
The Python source code goes through the following to generate an executable code :
• Step 1:
• The python compiler reads a python source code or instruction.
• Then it verifies that the instruction is well-formatted, i.e. it checks the syntax of each line.
• If it encounters an error, it immediately halts the translation and shows an error message.
• Step 2:
• If there is no error, i.e. if the python instruction or source code is well-formatted then the
compiler translates it into its equivalent form in an intermediate language called “Byte code”.
• Step 3:
• Byte code is then sent to the Python Virtual Machine(PVM) which is the python interpreter.
• PVM converts the python byte code into machine-executable code.
• If an error occurs during this interpretation then the conversion is halted with an error
message.
Python Interpreter and its working
• The python code you write is compiled into python byte code, which creates file
with extension .pyc .
• The byte code compilation happened internally, and almost completely hidden
from developer.
• Roughly, each of your source statements is translated into a group of byte code
instructions.
• This byte code translation is performed to speed execution byte code can be run
much quicker than the original source code statements.
• The .pyc file , created in compilation step, is then executed by appropriate virtual
machines.
• The Virtual Machine is the runtime engine of Python and it is always present as
part of the Python system, and is the component that truly runs the Python scripts .
• Technically, it’s just the last step of what is called the Python interpreter.
Syntax and Semantics
• The syntax of a programming language refers to the order to which
different elements are combined to from valid expressions.
• These elements may be words, operators, or phrases.
• The syntax of the Python programming language is the set of rules that defines
how a Python program will be written and interpreted
• The Python language has many similarities to Perl, C, and Java
• Semantics emphasizes the meaning of a program, so it'll be
understandable and easy to predict the outcome of execution.
• Semantics provides significant information needed to understand a program
Syntax and Semantics
Syntax and Semantics
Variables
The rules to name Variables
• The first character of the variable must be an alphabet or underscore ( _ ).
• All the characters except the first character may be an alphabet of lower-case(a-z),
upper-case (A-Z), underscore, or digit (0-9).
• Identifier name must not contain any white-space, or special character (!, @, #, %,
^, &, *).
• Identifier name must not be similar to any keyword defined in the language.
• Identifier names are case sensitive; for example, my name, and MyName is not the
same.
• Examples of valid identifiers
• a123, _n, n_9, etc.
• Examples of invalid identifiers
• 1a, n%4, n 9, etc.
Variables
• Python does not bind us to declare a variable before using it in the
application.
• It allows us to create a variable at the required time.
• We don't need to declare explicitly variable in Python.
• When we assign any value to the variable, that variable is declared
automatically.
• The equal (=) operator is used to assign value to a variable.
Variables
• Python is the highly object-oriented programming language; that's
why every data item belongs to a specific type of class.
• Consider the following example.
Variables
• In Python, every
created object
identifies uniquely in
Python.
• Python provides the
guaranteed that no
two objects will have
the same identifier.
• The built-in id()
function, is used to
identify the object
identifier. Consider
the following
example.
Variables
• Multiple Assignment
• Python allows us to assign a value to multiple variables in a single statement,
which is also known as multiple assignments.
• We can apply multiple assignments in two ways
• Assigning a single value to multiple variables
• Assigning multiple values to multiple variables.
• Consider the following example.
Variables
• Local Variable
Variables
• Global Variables
• A variable declared
outside the function
is the global variable
by default.
• Python provides the
global keyword to
use global variable
inside the function.
• If we don't use the
global keyword, the
function treats it as a
local variable.
Variables
• Delete a variable
• We can delete the variable using the del keyword
Built-in Data Types
Built-in Data Types
Built-in Data Types
Built-in Data Types
• Setting the Specific Data Type
• If you want to specify the data type, you can use these constructor functions
Built-in Data Types
Ordered vs Unordered
• If we look at the output for strings, lists
and tuples, they are in same order as
they are specified initially.
• And these data structures guarantee this
order.
• So strings, lists and tuples are ordered
collections of objects.
• If we look at the result of sets, initial
order, the order in which we specified
the elements, is not maintained.
• So sets and dictionaries are unordered
collections of objects.
Immutable
Built-in Data Types
Built-in Data Types
Built-in Data Types
• In Python, frozenset is the
same as set except the
frozensets are immutable
which means that elements
from the frozenset cannot be
added or removed once
created.
• frozenset(): This function
takes input as any iterable
object and converts them into
an immutable object. The
order of elements is not
guaranteed to be preserved.
String Operations
String Operations
String Operations
String Operations
count() method takes maximum
of three parameters:
• sub - It is the substring to be
searched in the str string.
• start and end (optional) - The
range str[start:end] within
which substring is searched.
String Operations
find() method takes maximum of
three parameters:
• sub - It is the substring to be
searched in the str string.
• start and end (optional) - The
range str[start:end] within
which substring is searched.
String Operations
• Only difference between
find() and index() is
that the index() method
raises an exception if
the value is not found.
String Operations
replace() method replaces a specified phrase with another specified phrase.
Syntax: string.replace(oldvalue, newvalue, count)
String Operations
• endswith() method returns True if the string ends with the specified
value, otherwise False.
• Syntax: string.endswith(value, start, end)
String Operations
• startswith() method returns True if the string starts with the specified
value, otherwise False.
• Syntax: string.startswith(value, start, end)
String Operations
• String Formatting
String Operations
• join() method takes all items in an iterable and joins them into one
string
String Operations
String Operations
String Operations
• split() method splits a string into a list
• Syntax: string.split(separator, maxsplit)
String Operations
• splitlines() method splits a string into a list
String Operations
• lstrip() method removes any leading characters spaces characters
• rstrip() method removes any trailing characters spaces characters
• strip() method removes any leading and trailing spaces characters
String Operations
• zfill() method adds zeros (0) at the beginning of the string, until it
reaches the specified length
Multiline String
String Operations
• Membership Operators
• To check if a certain phrase or character is present in a string, we can use the
keyword in.
• To check if a certain phrase or character is NOT present in a string, we can use
the keyword not in.
String Operations
• Escape Characters
Numeric Data Types
Arithmetic Operators
Bitwise Operator
Membership Operators