You are on page 1of 23

KEYWORDS, IDENTIFIERS AND

DATA TYPES IN PYTHON

Rounaq Choudhuri
rounaqchoudhuri@gmail.com
Indian Cyber Security Solutions

Lecture II Python Programming


Agenda for Lecture II
2

 Commenting
 Keywords and Identifiers
 Numbers
 Strings
 Multiple Assignments

Indian Cyber Security Solutions


Commenting Python
3

 Comments in Python are marked with the “#” symbol.


 Comments are used to annotate notes or other information
without having Python try to perform an operation on them.

SCRIPT OUTPUT

Indian Cyber Security Solutions


Python Identifiers
4

 A Python Identifier is the name used to identify a


variable, function, class, module or other object
 An identifier starts with a letter A to Z or a to z or
an underscore (_) followed by zero or more
letters, underscores and digits (0 to 9)
 The python does not allow punctuation
characters and special characters such as @, $,
and % within identifiers
 Python is case sensitive programming language

Indian Cyber Security Solutions


Python Identifiers
5

Naming Convention of Identifiers:


 Class names start with an uppercase letter. All other
identifiers start with a lowercase letter (ex. class
Person)
 Starting an identifier with a single leading underscore
indicates that the identifier is private (ex. def _Emp() )
 Starting an identifier with two leading underscores
indicates a strongly private identifier (ex. def __Emp() )
 If the identifier also ends with two trailing underscores,
the identifiers is a language-defined special name (ex.
def __init__() )

Indian Cyber Security Solutions


Python Identifiers
6

When running the Python interpreter in interactive mode,


a single underscore character(_) is a special identifier that
holds
the result of the last expression evaluated.

Indian Cyber Security Solutions


Python Keywords
7

 Keywords are special reserved words which convey a


special meaning to the compiler/interpreter
 Python Keywords cannot be used instead of Python
Identifiers

Indian Cyber Security Solutions


Python Keywords
8

print not
is

assert

or and

del

exec

Indian Cyber Security Solutions


Python Keywords
9

 We will learn more about if, elif, else, for, while,


break, continue, pass and in keywords in Flow
control.
 We will learn more about import, from, as, try,
except, raise and finally keywords in Modules &
Exceptions.
 We will learn more about lambda, def, return and
yield keywords in Functions.
 We will learn more about class and global
keywords in Classes and Objects.
 We will learn more about with keyword in File I/O.
Indian Cyber Security Solutions
Data Types
10

 Python is a dynamically typed language


DATA DESCRIPTION MUTABILIT EXAMPLE
TYPE Y
Number Stores numeric values Immutable 5, 6, etc.
String Sequence of characters Immutable “This is a
String”
List List of elements. The elements Mutable [1,”Hello”,
may be of different data types True]
Tuple List of elements. The elements Immutable (1,”Hello”,
may be of different data types True)
Dictionary A list of items indexed by keys. Mutable {“Name”:”Ra
Keys are unique and non- hul”,
mutable “Age”:26}

Indian Cyber Security Solutions


Numbers
11

 Integer Numbers and Floating-point Numbers


Data Types Examples

Integer -2, 1, 0, -99

Floating – point 1.25, -1.0, -0.5, 0.0, 0.5,

 type(object) method  The type(object) is


python in-built method that returns the data type
of an object.

Indian Cyber Security Solutions


Examples(Numbers)
12

 Integer numbers and floating-point numbers can


be stored in variables and kept for later
operations

 Integer numbers and Floating-point numbers can


be used to operate on each other

Indian Cyber Security Solutions


Numbers
13

 Python also supports two other types of


numbers
 Long integers

 Complex numbers
• They are written in the form of:
a + bJ

Indian Cyber Security Solutions


Strings
14

 Strings in programming are simply text,


either individual characters, words,
phrases, or complete sentences.
 They are sequence of characters(string
of characters)

Indian Cyber Security Solutions


Strings
15

 They are written within single quotes(‘ ’)


and double quotes(“ ”)

 They can be also written within triple


single quotes(‘’’ ‘’’) and triple double
quotes(“”” “””)

Indian Cyber Security Solutions


Escaping Sequences
16

 A sequence of characters that starts with


a backslash is known as an escape
sequence.
 These are used to enter characters that
areESCAPE
not usually recognized
SEQUENCE by the
DESCRIPTION
interpreter.
\n New line
\t Tab space
\\ Backslash
\’ Single quote
\” Double quote

Indian Cyber Security Solutions


String Indices and Slicing
17

 Strings are arrays of characters and


elements of an array can be accessed
using indexing.
 Indices start with 0 from
left side and -1 when start
from right side.
 To cut a substring from a
string is called string slicing.

Indian Cyber Security Solutions


String Formatting or
18
Interpolation
 String formatting or string interpolation
is dynamic putting of various values into
a string.
 Either the ‘%’ symbol or the {} and the
format() method is used.

Indian Cyber Security Solutions


String methods
19

 These methods are provided by the


python interpreter.
METHOD DESCRIPTION
len(string) Returns the length of the string
string.strip() Strips the string of any extra spaces on the RHS
and LHS
string.split() Splits the string into a list using a delimiter
(default delimiter=“ “)
string.count(substr Returns the number of times the substring
ing) occurs in the string
string.find(substrin Returns the index of the substring
g)
‘ ’.join(string) Joins the characters using “ “
Indian Cyber Security Solutions
string.swapcase() Swaps lowercase for uppercase and vice versa
Concatenation and
20
Replication
 Two strings can be concatenate using
the ‘+’ operator.

 Replication It is the process by a string


is replicated several times

Indian Cyber Security Solutions


Boolean and ‘None’ values
21

 Python also supports two kinds of values:


 Boolean values True and False

 ‘None’

Indian Cyber Security Solutions


Multiple Assignments
22

 Multiple python variables can be assigned values


in a single statement

Indian Cyber Security Solutions


Question
23

Question???

Indian Cyber Security Solutions