Python PPT 2
Python PPT 2
Python PPT 2
• Statements in Python typically end with a new line. Python, however, allows the use of the line
continuation character (\) to denote that the line should continue. For example
total= item_one + \
item_two + \
item_three
• The statements contained within the [], {}, or () brackets do not need to use the line continuation character.
For example
days= ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
2
Standard Data Types
3
Python Numbers
Number data types store numeric values. Number objects are created when you assign a
value to them. For example
var1= 1
var2 = 10
You can also delete the reference to a number object by using the del statement. The syntax
of the del statement is −
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement.
For example
del var
del var_a, var_b
Python supports three different numerical types −
• int (signed integers) eg: 100
• float (floating point real values) eg: 32.3+e18
• complex (complex numbers) eg: 9.322e-36j
4
Data Type Conversion
•Sometimes, you may need to perform conversions between the built-in types. To
convert between types, you simply use the type-name as a function.
•There are several built-in functions to perform conversion from one data type to
another. These functions return a new object representing the converted value.
5
Data Type Conversion
6
Basic Operators
Types of Operator
7
Python Arithmetic
Operators
8
Python Comparison Operators
9
Python Assignment
Operators
Assume variable a
holds 10 and variable
b holds 20
10
Python Bitwise Operators
Assume if
a = 60; and
b = 13;
a= 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
11
Python Logical Operators
Assume variable
a holds True and
variable b holds False
12
Python Membership Operators
Python’s membership
operators test for
membership in a
sequence, such as
strings, lists,
or tuples.
13
Python Identity Operators
Identity operators
compare the memory
locations of two objects.
There are two Identity
operators as explained
below:
Line 1 a= 20 : 1594701888
b= 20 : 1594701888
Line 2 - a and b have same identity
14
Python Operators
Precedence
15
Python Operators
Precedence
16
Python Programming Development Cycle
17
Interacting with Python
18
Starting the Interpreter
In Windows, there will likely be a program group in the Start menu
labeled Python 3.x, and under it a menu item labeled Python 3.x (32-
bit), or something similar.
Clicking on that item will start the Python interpreter:
19
Command Line
•In Windows, it is called Command Prompt.
•In macOS or Linux, it should be called Terminal.
20
Executing Python Code
1. Ensure that the >>> prompt is displayed, and the cursor is
positioned after it.
2. Type the command print("Hello, World!") exactly as shown.
3. Press the Enter key.
21
Exiting the Interpreter
When you are finished interacting with the interpreter, you can exit a
REPL session in several ways:
•Type exit() and press Enter:
•In Windows, type Ctrl+Z and press Enter:
22
Running a Python Script from the
Command Line
•Windows: Notepad
•Unix/Linux: vi or vim
•macOS: TextEdit
Using whatever editor you’ve chosen,
create a script file
called hello.py containing the
following:
23
Interacting with Python through an
IDE
IDEs usually provide capability as well as an editor with which you can
create and modify code to then submit to the interpreter for execution.
25
Features of IDLE
The interpreter behaves more or less the same as when you ran it directly from the
console. The IDLE interface adds the perk of displaying different syntactic elements in
distinct colors to make things more readable.
It also provides context-sensitive help. For example, if you type print( without typing any
of the arguments to the print function or the closing parenthesis, then flyover text should
appear specifying usage information for the print() function.
26
Features of IDLE
You can also create script files and run them in IDLE. From the Shell window menu,
select File → New File. That should open an additional editing window. Type in the code
to be executed.
From the menu in that window, select File → Save or File → Save As… and save the file to
disk. Then select Run → Run Module. The output should appear back in the interpreter
Shell window:
27
Numbers
Numbers are Immutable objects in Python that cannot change their
values.
There are three built-in data types for numbers in Python3:
•• Integer (int) numbers (float)
Floating-point
•Complex numbers: <real part> + <imaginary (not used much in Python
programming)
part>jNumber
Common
Functions
Function Description
int(x) to convert x to an integer
float(x) to convert x to a floating-point number
abs(x) The absolute value of x
cmp(x,y) -1 if x < y, 0 if x == y, or 1 if x > y
exp(x) The exponential of x: ex
log(x) The natural logarithm of x, for x> 0
pow(x,y) The value of x**y
sqrt(x) The square root of x for x > 0
28
Numbers
29
Numbers
30
Strings
Python Strings are Immutable objects that cannot change their
values.
String indexes starting at 0 in the beginning of the string and working their way from -
1
at the end.
31
Strings
String Formatting
+ Concatenation - Adds values on either side of the operator a + b will give HelloPython
* Repetition - Creates new strings, concatenating multiple copies a*2 will give HelloHello
of the same string
[] Slice - Gives the character from the given index a[1] will give e
a[-1] will give
o
[:] Range Slice - Gives the characters from the given range a[1:4] will give ell
in Membership - Returns true if a character exists in the given string ‘H’ in a will give True
32
Strings
33
Strings
34
Strings
• "hello"+"world" "helloworld" # concatenation
• "hello"*3 "hellohellohello" # repetition
• "hello"[0] "h" # indexing
• "hello"[-1] "o" # (from end)
• "hello"[1:4] "ell" # slicing
• len("hello") 5 # size
• "hello" < "jello" 1 # comparison
• "e" in "hello" 1 # search
• "escapes: \n etc, \033 etc"
• 'single quotes' """triple quotes""" r"raw strings"
35
Strings
Common String
Methods
Method Description
str.count(sub, Counts how many times sub occurs in string or in a substring of
beg= string if starting index beg and ending index end are given.
0,end=len(str))
str.isalpha() Returns True if string has at least 1 character and all characters
are alphanumeric and False otherwise.
str.isdigit() Returns True if string contains only digits and False otherwise.
str.lower() Converts all uppercase letters in string to lowercase.
str.upper() Converts lowercase letters in string to uppercase.
str.replace(old, new) Replaces all occurrences of old in string with new.