Chapter 1 Intro To Python
Chapter 1 Intro To Python
PYTHON
CHARACTER
SET
What is Token?
1. Key
Words
5. 2.
Punctuators Identifiers
TOKENS
4.
Operators. 3. Literals
What is Keyword or reserved word?
1. Keyword/Reserved Word
What is Keyword?
and assert
break class
continue def
del elif
else except
exec finally
for from
Some Keywords of Python Language
global if
import in
is lambda
not or
pass print
raise return
try while
with yield
What is an identifier?
2. IDENTIFIERS
What is an identifier?
A Python Identifier is a name
given to a function, class, variable,
module, or other objects that you’ll be
using in your Python program.
In short, its a name appeared in
the program.
For example: a, b, c
a b and c are the identifiers and
a b & c and , are the tokens
PYTHON NAMING CONVENTIONS
OR
5. Python is a case-sensitive
language and this behaviour extends to
identifiers. Thus, Labour and labour are
two distinct identifiers in Python.
6. You cannot use Python keywords as
identifiers.
PYTHON NAMING CONVENTIONS
What is literals?
What is string?
Sequence of letters enclosed in
quotes is called string or string literal or
constant.
Python supports both form of
quotes i.e.
‘Hello’
“Hello”
Representation of String
REPRESENTATION OF STRING
Forward Indexing
REPRESENTATION OF STRING
‘\\’ Size is 1 (\ is an
escape sequence)
‘abc’ size is 3
“\ab” size is 2
“Raama\’s Laptop” size is 13
Strings with Triple Quotes
STRINGS WITH TRIPLE QUOTES
\\ Back Slash
\a ASCII Bell
\b ASCII Backspace
\f ASCII Formfeed
ESCAPE SEQUENCES
\n New Line
\r Carriage return
\t Horizontal Tab
ESCAPE SEQUENCES
\t Vertical Tab
Float type
BOOLEAN LITERALS OR CONSTANTS.
3) BOOLEAN LITERALS OR CONSTANTS.
In computer terminology
statement refers to an instruction.
What is function?
Function is a self contained
program segment which carries out
some specific well defined task.
For Example:
def sqr( num ):
result= num *num
print ("Square = " ,
result)
sqr()
PYTHON PROGRAMMING CONVENTIONS
PYTHON PROGRAMMING CONVENTIONS
78 79 80 81 82 83 84 85 86 87
2000 2016 2018 2026 2032 2044 2048 2050 2054 2068
marks refers to
location 2054
VARIABLES AND ASSIGNMENTS
Now marks = 81
78 79 80 81 82 83 84 85 86 87
2000 2016 2018 2026 2032 2044 2048 2050 2054 2068
marks refers to
location 2026
Multiple Assignments
Python is very versatile with
assignment statements.
Multiple Assignments
Multiple Assignments
Multiple Assignments
Expressions separated by commas
are evaluated from left to right.
Now,
x = 10
y,y = x+2,x+5
y,y = 12,15
First It will assign y = 12 then y = 15
So print(y) will print 15
VARIABLES AND ASSIGNMENTS
Dynamic Typing:
A variable pointing to a value of certain
type can be made to point to a value/object
of different type this is called Dynamic
Typing.
x=10
print(x)
x=“ Hello World”
print(x)
VARIABLES AND ASSIGNMENTS
Output will be
10
x
Hello World
10
10
Hello World
VARIABLES AND ASSIGNMENTS
x = ‘day’
y = x/2 Error! String can not be divided.
VARIABLES AND ASSIGNMENTS
type() function:
type() function:
Variable_to_hold_the_value=input(message)
For Example:
Where,
variable_to_Hold_the_Value is a variable
which is the label for a memory location
where the value is stored.
INPUT ( ) FUNCTION
For Example:
p = input(“Enter the value”)
x = int(input(“Enter x value”))
reads the value and converts it in to integer type
data or value.
y=float(input(“Enter y value”))
reads the value and converts it in to float type
data or value.
INPUT ( ) FUNCTION
print() Parameters:
objects - object to the printed. * indicates that
there may be more than one object
sep - objects are separated by sep. Default
value: ' ‘ end - end is printed at last
file - must be an object with write(string)
method. If omitted it, sys.stdout will be used
which prints objects on the screen.
flush - If True, the stream is forcibly
flushed. Default value: False
PRINT ( ) FUNCTION
Example 1: How print() works in Python?
print("Python is fun.")
a=5
#Two objects are passed:
print("a =", a)
b=a
# Three objects are passed:
print('a =', a, '= b‘)
Output
Python is fun.
a=5
a=5=b
PRINT ( ) FUNCTION