Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Python Basics

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python Basics

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

INTRODUCTION TO PYTHON

Agenda
⚫ Why Python?
⚫ What is Python?
⚫ Python Applications
⚫ Python Installation
John please tell me with
It has to be Python, let me
which programming
give you enough reasons
language should I start
to believe that
with?
Different Programming Languages
Why Python?

• Python is simple and easy to learn, read


Simple to use
and write

• Python is an Open Source software


Open-Source which means one can freely read,
modify, distribute it’s source code.

• Supported by many platforms like Linux,


Portable
Windows, FreeBSD, Macintosh, etc.
Why Python?
• Python supports object-oriented
Object-Oriented
language and concepts of classes,
Language
objects encapsulation, etc.

• Python provides a rich set of modules


and functions so you do not have to
Large Standard Library
write your own code for every single
thing

Dynamically Typed • Python is a dynamically-typed language


Language as we discussed earlier.
What is Python?
Python is a general purpose, high level, interpreted and
dynamic type language created by Guido Van Rossum in 1991.

General Purpose

High Level

Interpreted

Dynamic Type
High Level Programming
Interpreter
An interpreter a translator used to translate just one statement
of the program at a time into machine code.

Input Output
High-Level Machine/
Interpreter
Language Low-level language
Dynamic Typing
⚫ Dynamic typing means that the type of the variable is
determined only during runtime.
⚫ Whereas static typing used by few other languages
requires users to declare the variable type.
Dynamic Typing

• name = “John”

Static Typing

• String name = “John”


Who is using Python
Dropbox storage service The RaspberryPi –single
The popular Youtube video Google makes extensive
codes both his server and board computer
sharing system is largely use of Python in it’s web
client s/w primarily in promotes Python as its
written in Python. search system.
Python educational language

COMPANIES THAT USE PYTHON

Bit-Torrent peer-to-peer Netflix and Yelp both


NASA uses Python for NASA uses Python for
file sharing system began have documented the
specific programming cryptography and
its life as a Python role of Python in their
tasks. intelligence analysis.
program SW infrastructures.
Python Applications

• Web scraping refers to the extraction of


Web Scrapping
data from a website.

• Python offers many frameworks from


which to choose from including bottle.py,
Web Development
Flask, CherryPy, Pyramid, Django and
web2py.

• Python is commonly used as a


Data Analysis programming language to perform data
analysis
How to install Python
Alright, tell me
how to install
Python
Sure, its easy btw
Installation of Python
Python Installation
Python Installation
Python Interpreter
⚫ Python interpreter is a program that reads
and executes the code.
⚫ This includes the code , pre-compiled code
and scripts.

Source credi: https://medium.com


Jupyter Notebook Installation
• Install the classic Jupyter Notebook from
command prompt with:
pip install notebook
• To run the notebook:
jupyter notebook
• To pip installation
py –m ensurepip --upgrade
PYTHON GET STARTED
Agenda
• Demo of Hello world
• Comments and Literals
• Indentation
• Python Code Execution
Demo of Hello World
Every character in Python
Output after running
should be enclosed within
helloworld.py
single or double quotes

print(‘Hello World’); Hello World


print(“Welcome to KMIT”) Welcome to KMIT
Input and Output
⚫ To perform I/O task in Python there are two
built-in functions.
print()
input()

⚫ Widely used for standard input and output


operations respectively.
Taking Input in Python
⚫ Developers often have a need to interact with users,
either to get data or to provide some sort of result.
⚫ Python provides us with inbuilt function to read the
input from the keyboard.
⚪ input ( prompt )
⚫ The input() function takes a single optional
argument
⚫ prompt (Optional) - a string that is written to
standard output (usually screen) without trailing
newline
Comments and Literals
Comments: Any text to the right of # symbol is mainly used as notes for
the readers.
Statements on right side of # doesn’t get executed. It gives us more
information about function
Bulk Comments: Enclose the code in triple quoted strings(“””)
Literal Constants: Any number or character, or set of characters
#Comment

‘‘‘
Bulk Comments
Multiline Comments
‘‘‘

2
Indentation
• In simple terms indentation refers to adding
white space before a statement.

Source credit: https://www.geeksforgeeks.org/


Indentation
• Indentation Rules:

• The first line of python code cannot have


Indentation.
• Indentation is mandatory in python to define the
blocks of statements.
• The number of spaces must be uniform in a block
of code.
• It is preferred to use whitespaces instead of tabs to
indent in python.
Variables
How do you think the value is assigned??

Assigning multiple values Assigning a single value

Input Input

>>>a=10
a, b = 100, 200
>>>name=‘Nikitha’
print(a)
>>>salary=200.3
# 100
>>>print (a)
print(b)
>>>print(name)
# 200
>>>print(salary)
Tokens

Keywords

Identifiers

Literals

Operators
Keywords
⚫ Python keywords are special reserved words
⚫ Conveys a special meaning to the compiler/interpreter
⚫ Each keyword have special meaning and special
operation
⚫ NEVER use it as a variable.

Source credit: https://www.geeksforgeeks.org/


Identifiers
⚫ Identifiers are the name used to identify a variable,
function, class or an object.

⚫ Rules defined for naming and identifier


⚫ An identifier starts with a letter A to Z or a to z or an underscore (_)
followed by zero or more letters.
⚫ It cannot start with digits and special characters.
⚫ Python is case-sensitive, and so are Python identifiers. Name and name
are two different identifiers.
Literals
• Literal is a raw data given in a variable or constant.
String Literal
Numeric Literal
Special Literal
Boolean Literal
String Literal
• A string literal is a sequence of characters
surrounded
strings = by quotes.
"This is Python"
• We can=use
char both single, double or triple quotes
"C"
for a string.
multiline_str = """This is a multiline
• And, a character literal is a single character
string with more than one line code.
surrounded by single or double quotes.
"""
Numerical Literal
a = 0b1010 #Binary Literals
• Numeric Literals
b = 100are immutable
#Decimal Literal(unchangeable).
c = 0o310 #Octal Literal
• Numeric literals can#Hexadecimal
d = 0x12c belong to 3 different
Literal numerical types
– Integer,
– Float, #Float Literal
– Complex.float_1 = 10.5
float_2 = 1.5e2

#Complex Literal
x = 3.14j

print(a, b, c, d)
print(float_1, float_2)
print(x, x.imag, x.real)
Boolean Literal
• A Boolean literal can have any of the two values
⚪ True
⚪ False x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10

print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
Special Literals
• Python contains one special literal i.e. None. We
use it to specify to that field that is not created.
drink = "Available“
food = None
x=drink
if x == drink:
print(food)
else:
print(drink)
Printing Output
• We use the print() function to output data to
the standard output device (screen).
• Syntax :
objects is the value(s) to be printed.

print(*objects, sep=' ', end='\n', file=sys.stdout,


flush=False)
sep separator is usedinto
It defaults between
a newthe values
line.
specifying if the output is flushed (True)
or buffered (False). Default is False
The file is the object where the values are printed and its
default value is sys.stdout (screen).
OPERATORS IN PYTHON
Operators
• In computer programming languages operators
c = a+b
are special symbols which represent
a and b are called
computations, conditional matching etc. operands
+ is an operator
• The values the operator uses are called
operands.
Types of Operators

Source credit: https://techvidvan.com


Arithmetic Operators

Source credit: https://techvidvan.com


Arithmetic Operators

Operator Name Example


+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Relational Operators

Source credit: https://techvidvan.com


Relational Operators
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Logical Operators
Operator Description Example

and Returns True if both x < 5 and x < 10


statements are true

or Returns True if one of x < 5 or x < 4


the statements is true

not Reverse the result, not(x < 5 and x <


returns False if the 10)
result is true
Special Operators
• Identity Operator

• Membership Operator

Keshav Memorial Institute of


Technology
Identity Operator
• is and is not are the identity operators in Python.
• They are used to check if two values (or variables)
are located on the same part of the memory.
• Two variables that are equal does not imply that they
are identical.

Source credit: w3schools.com


Example-1
a = 20 Output:
b = 20 a and b have same identity
a and b have same identity
if ( a is b ):
print("a and b have same identity")
else:
print("a and b do not have same identity")

if ( id(a) == id(b) ):
print("a and b have same identity")
else:
print("a and b do not have same identity")
Example-2
a=20
c=20.0 Output:
a and c do not have same identity
if a is c: a and c are equal
print("a and c have same identity")
else:
print("a and c do not have same identity")

if a == c:
print("a and c are equal")
else:
print("a and c are not equal")
Membership Operator
• in and not in are the membership operators in Python.

• They are used to test whether a value or variable is found in a


sequence (string, list, tuple, set and dictionary).

• In a dictionary we can only test for presence of key, not the value.
Operator Precedence

Operator precedence determines the grouping of


terms in an expression and decides how an
expression is evaluated.
Operators Meaning
() Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not
Comparisons, Identity, Membership operators
in
not Logical NOT
and Logical AND
or Logical OR
Self Check
a = 20
b = 10
c = 15
d=5
e=0

e = (a + b) * c / d
print ("Value of (a + b) * c / d is ", e)

e=a+b*c/d
print("Value of a + b * c / d is ", e)

e = (a + b) * (c / d)
print("Value of (a + b) * (c / d) is ", e)

e = a + (b * c) / d
print("Value of a + (b * c) / d is ", e)
Operator precedence
Determine the hierarchy of operations and evaluate the
following expression:
i = 2 * 3 // 4 + 4 // 4 + 8 - 2 + 5 // 8
Stepwise evaluation of this expression is shown below:
⮚ i = 2 * 3 // 4 + 4 // 4 + 8 - 2 + 5 // 8
⮚ i = 6 // 4 + 4 // 4 + 8 - 2 + 5 // 8 operation: *
⮚ i = 1 + 4 // 4 + 8 - 2 + 5 // 8 operation: //
⮚ i = 1 + 1+ 8 - 2 + 5 // 8 operation: //
⮚ i = 1 + 1 + 8 - 2 + 0 operation: / /
⮚ i = 2 + 8 - 2 + 0 operation: +
⮚ i = 10 - 2 + 0 operation: +
⮚ i = 8 + 0 operation : -
⮚ i = 8 operation: +
Thank you.

You might also like