R.B.BHARATHI
B.sc computer science“A”sec
21CSA09
Literals in python
Determining the datatype of a variable
What about characters
user-defind Datatypes
Constant in python
Identifiers and Reserved words
Naming conventions in python
3.
LITERALS IN PYTHON
•A literal is a constant value that is stored into a variable
in a program.
observe the folloeing statement:
a=15
•Here, ‘a’is the variable into which the constant value 15 is stored.hence the
value ‘15’ is called literal .
since 15 indicates integer value’it is called integer literal .
4.
types of literalsin python:
Numeric iterals
Boolean literals
String literals
Numeric literals:
These literals represent numbers.
The differnt types if numeric literals available in python.
Boolean literals:
Boolean literalsare the TRUE or
FALSE valucs stored into a bool type variable.
String literal:
• A group of characters is called a string literal.
• These string literals are enclosed in single quotes(‘)or double
quotes(“)or triple quotes(‘‘‘or””’)in python.
A single or double quoted strings should end in the same line
as:
s1= ‘this is first Inian book’
s2= “core python”
7.
The stringhave taken up to3 lines.
Hence, we inserted them inside triple single quotes( ‘‘‘)or double
quotes(“““).
String spans more then one line adding backslash ()will join the next string
to it for example,
str=“this is first line and
this will be continued with the first line”
print(str)
OUTPUT:
This is first line and this will be continued with
the first line
8.
we can usen inside a string literal .
for example,
str=”this is pythonn program”
print(str)
OUTPUT:
This is python
program
whenn is written,it will throw the cursorto the new line.
9.
ESCAPE CHARACTER MEANING
New line continuation
Display a single
” Display a double quotes
b Backspace
r Enter
t Horizontal tab space
v vertical tab
n New line
Important escape characters in string
10.
Determining the datatypeof a variable:
data type of a variable or object,we can use type() function.
type (a) displays the datatype of the variable ‘a’
a=15
print(type(a))
<class ‘int’>
we are storing an integer number 15 into variable ‘a’,the type of ‘a’ is
assumed by python interpreter as ‘int’.
‘int’ is treated as a class.
every datatype ,function,method,class,module,list,sets,etc...are all objects in
python.
What about characters
A character,we should think of it as a element in a string.
in following sattements,we are assigning a single character ‘A’to a ‘ch’.
Then we find out the type of the ‘ch’ variable using the type() function.
ch=‘A’
print(type(ch))
<class ‘str’>
13.
‘ch’ is displaysas ‘str’ type.we have to access the individual characters of a string
using index or position numbers.string str where we stored a string literal
‘bharath’as:
str= ‘Bharath’
print(str[0])
B
To retrieve all the characters using a for loop,we can write:
To retrieve all the characters using a for loop,we
can write;
for i in
str:print(i)
14.
Display the charactersas:
B
h
a
r
a
t
h
Let’s check the case of first two letters in‘Bharath’a:
str[0].isupper()
True
str[1].isupper()
False
15.
user-defind Datatypes
Thedatatypes which are created by the programmers are called
‘user-defined’ dataypes.
for example,
Aarray,a class,or a module is user-defind datatypes.
Constants in python
A constant is similar to a variable but its value modified
or changed in the course of the program execution.
for examble, MAX_VALUE is a contant .
But its value can be changed.
16.
Identifiers and Reservedwords
An identifier is a name that is given to a variable or function or class etc.
Identifiers can include letters,numbers,and the underscore character(_).
special symbols such as,
?,#,$,%,@ are not allowed in Identifiers.
some exambles for identifier are slary,name11,gross-income,etc.
17.
salary = 15000.75
Thisis stores right side literal into left side value.
thus it is performing an operation.
Hence it is called ‘operator’.
This is a variable.
its name is ‘salary’.
Hence this name ‘salary
is ‘identifier’
This is the float number stored into
variable.
hence its called ‘floating point ‘literal’
Shows exambles of a variable ,an operator and literal
18.
Reserved words availablein python:
and
as
assert
break
class
continue
def
del
elif
else
except
exec
finally
for
Naming conventions inpython
packages:
• packages names should be written in all lower case letters.
• when multiple words are used for a name,we should them using an
underscore(_).
The rules related to writing names of packages,
modules,classes,variables,etc
are called naming conventions.
21.
modules:
• modules namesshould be written in all lower case letters.
• when multiple words are used for a name, we should separate them using
underscore(_).
classes:
• Each word of a calss name should start with capital letter.
• This rule is application for the classes created by us. python’s built-in class
names use all lowercase words. when a class represents exception,then its
name should end with a word ‘Error’.
22.
global variable ormodule-level variable:
Global variables names should be all lower case letters. when multiple words
are used for a name,we should separate them using an underscore(_)
Functions:
• function name should be all lower case letters.
• when multiple words are used for a name,we should separate them using an
underscore(_).
23.
methods:
• method nameshould be all lower case letters.
• when multiple words are used for a name,we should separate them using an
underscore(_).
method arguments:
In case of instance method , their first argument name should be self
in case of class methods ,their first argument nme should be cls
constants:
constants names should be written in all capital letters. if a constant has
several words, then each word should be separated by an underscore(_).