
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























Examples
Name = “Ngawang”
Age = 25
City = “Thimphu”
Is_student = True
Subject = “PGL101”
(^) A variable name can contain letters (A-Z, a-z), digits (0- 9 ) and underscore ( _ ).
(^) A variable name must start with a letter or underscore ( _ ), not with a digit.
(^) A variable name cannot be a Python keyword. (like: if, else, for, while etc).
(^) Spaces are not allowed in variable names.
(^) Variable names are case-sensitive. (age, Age and AGE are different)
(^) Use meaningful and descriptive names(Example: total_marks instead of a)
(^) Avoid using special characters except underscore ( _ ). (e.g. @, #. %, &, $,
are not allowed)
An identifier is a name that is given to a variable or function or class etc.
Identifier can include letter, number and underscore ( _ ) character.
They should always start with a nonnumeric character.
Example : salary, name11, gross_income etc.
Special symbols such as ?, #, $, %, @ and etc. are not allowed in
identifier
A literal is a constant value that is stored into a variable in a program.
a = 15
Here ‘a’ is a variable into which the constant value ‘15’ is store. Hence the value
‘15’ is called ‘Literal’.
This is a variable. Its name is
‘salary”. Hence this name
‘salary’ is identifier.
This symbol store right side literal into
left side variable. Thus, it is performing
an operations. Hence it is called
‘Operator’
This is the float number stored
into variable. Hence it is called
‘floating point literal’ Constant
A constant is similar to a variable, but its value cannot be change or modified during
the program execution.
eg: import math
math.pi
Defining the constant is not possible in python.
The datatypes which can be
created by the programmer is called
User-define datatypes.
The datatypes which are already
available in Python language
There are FIVE types of Built-in
datatypes, such as:
Numeric, None Type, Sequences,
Mapping and Set.
Examples: Array, class or
Module
It represent an object that does
not contains any value.
We store ‘None’ to represent an
empty object.
In Boolean expression, ‘None’
value represents ‘False’
Examples:
User_statuse =
None
It represent number. There are THREE
types.
int
Float
complex
2.1.int Datatype: It represents an integer number.
An integer number is a number
without any decimal/fraction
part,
Example:
age =
A sequence represent a group of
elements or item.
There are SIX types of sequences.
str list
bytes tuple
byte array range
str represent string datatype. It is
represented by the group of character.
String are enclosed in single or double
quotes. Eg: name = “ sonam ”
3.2 range Datatypes
It represents a sequence of numbers. range()
is a function that generate a sequence of
number and returns them in an object.
So, range() is also called a ‘ generator
function’
Examples:
r = range( 10 )
for i in r :
print( i )
3.3. list Datatypes
List in Python are similar in C or Java. A list represents a group of elements.
The main difference between a list and array is that array can store only one types of
elements.
List can grow dynamically in memory, but the size of an array is fixed, and they cannot
grow at runtime.
It is represented using square brackets [ ] and the elements are written in [ ], separated by
comma.
Examples: List = [ ]
List = [ 10 ]
List =[ 10 , - 20 , element 15.5, ‘karma’, ”sonam” ] # different elements
3.5. byte Datatypes
A bytes Datatypes represents a group of byte numbers just like an array does. A byte
number is any positive integer from 0 to 255 (inclusive)
elements = [ 10, 20, 0, 40, 15 ]
x = bytes(elements)
print(x[0])
3.6. bytearray Datatypes
It is similar to bytes datatype. Th difference is that bytes type cannot be modified but
bytearray can be modified. It means any elements of bytearray can be modified.
elements = [ 10, 20, 0, 40, 15 ]
x = bytearray(elements)
print(x[0])
x[0] = 88 # replace 0 10
th element by 88
A set is the unorder collection of element. The order of the elements is not
maintained in the sets. It means the elements may not appear in the same order as
they entered the set.
It does not accept the duplicate elements.
There are TWO types of set Datatypes
4.2. frozen set Datatypes
It is same as set datatypes. The main difference is that the element in the set data
types can be modified whereas the element of the frozenset cannot be modified.
Example: s = { 10, 20, 30, 20, 50 }
print(s) #may display as { 50, 10, 20,
30 } fs = frozenset(s)
print(fs) # may display as
A map represents a group of elements in the form of key – value pairs so that
when the key is given, we can retrieve the value associated with it.
The key and its value should be separated by a colon ( : ) and every pair should be
separated by a comma.
All the elements should be enclosed inside curly
brackets { } Example: dict datatypes
The ‘dict’ represent a ‘dictionary’ that contains pairs of element (key and
value)
eg: d = { 10 : ‘karma’, 11 : ‘Pema’}
The programmer converts the data type using built-in function
Common casting functions:
Function Convert to Example
Int() Integer Int(“25”) 25
float() float float(“3.14”) 3.
str() string str(100) “100”
bool() Boolean Bool(1) True
List() list List((1,2,3)) [1,2,3]
Tuple() tuple Tuple([1,2,3]) (1,2,3)
Set() set Set([1,2,2,3]) {1,2,3}
Operators are special symbols that performs operations on one, two or more
operands and return a result.
Types of operators
Arithmetic Operators
(^) Comparison Operators
(^) Logical Operators
(^) Assignment Operators
(^) Bitwise Operators
(^) Membership Operators
(^) Identity Operators
Order of Operation :
modulus and floor division are
equal.