Submitted by
Mrs.R.Chinthamani,
Asst Professor,
Department of Computer Science,
E.M.G.Yadava Women’s College
PYTHON
ASSIGNMENT
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
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 .
types of literals in python:
 Numeric iterals
 Boolean literals
 String literals
Numeric literals:
 These literals represent numbers.
 The differnt types if numeric literals available in python.
EXAMPLES LITERAL NAME
450,-15 Integer literal
3.14,-70.88,1.25E4 Float literal
0x5A1c Hexadecimal literal
0557 Octal literal
0B11010101 Binary literal
18+3J Complex literal
Numeric literals
Boolean literals:
Boolean literals are 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”
 The string have 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
we can use n 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.
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
Determining the datatype of 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.
program
a=15
print(type(a))
<class‘int’>
a=15.5
print(type(a))
<class‘float’>
lst=[1,2,3,4]
print(type(lst))
<class‘list’>
s={6,7,8,9}
print(type(s))
<class‘s’>
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’>
‘ch’ is displays as ‘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)
Display the characters as:
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
user-defind Datatypes
 The datatypes 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.
Identifiers and Reserved words
 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.
salary = 15000.75
This is 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
Reserved words available in python:
and
as
assert
break
class
continue
def
del
elif
else
except
exec
finally
for
from
global
if
import
in
is
lambda
nonlocal
not
or
pass
print
raise
return
try
while
with
yield
false
true
Naming conventions in python
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.
modules:
• modules names should 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’.
global variable or module-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(_).
methods:
• method name should 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(_).
The Literals and Variables Concept  in Python.pptx

The Literals and Variables Concept in Python.pptx

  • 1.
    Submitted by Mrs.R.Chinthamani, Asst Professor, Departmentof Computer Science, E.M.G.Yadava Women’s College PYTHON ASSIGNMENT
  • 2.
    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.
  • 5.
    EXAMPLES LITERAL NAME 450,-15Integer literal 3.14,-70.88,1.25E4 Float literal 0x5A1c Hexadecimal literal 0557 Octal literal 0B11010101 Binary literal 18+3J Complex literal Numeric literals
  • 6.
    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.
  • 11.
  • 12.
    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
  • 19.
  • 20.
    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(_).