Tokens in PYTHON
Tokens in PYTHON
Jan 6, 2025
Tokens
A token is the smallest individual unit in a python
program.
All statements and instructions in a program are built
with tokens.
The various tokens in python are :
(i) Keywords
(ii) Identifiers
(iii) Literals
(iv) Operators
(v) Punctuators
Jan 6, 2025
Keywords (Reserved words)
Keywords are words that have some special meaning or
significance in a programming language.
Jan 6, 2025 3
Keywords (Reserved words)
Examples :
for print continue break
def if return del
import elif in while
else is and not
Python Identifiers
print(a)
print(b)
print(c)
Literals
Output:
Hello
World
Python is a
high-level and
general purpose language
Output:
None
Literals
Literals Collections:
Literals collections in python includes list, tuple, dictionary, and
sets.
List : It is a comma-separated list of elements enclosed in
square brackets. These elements can be of any data type and can
be changed.
Tuple : A comma-separated list of elements in round brackets is
known as a tuple. Values can be of any data type, but they
cannot be modified.
Dictionary: is an ordered collection of key-value pairs.
Set : is an unordered collection of objects enclosed in curly
braces.
Operators
These are the tokens responsible to perform an operation in
an expression.
The variables on which operation is applied are
called operands.
Operators can be unary or binary.
Unary operators are the ones acting on a single operand like
complement operator, etc.
While binary operators need two operands to operate.
Punctuators
These are the symbols that used in Python to organize the
structures, statements, and expressions.
underscores (_).
Variables names must start with a letter or an underscore.
A variable name cannot start with a digit.
Variable names are case-sensitive (myVar and myvar are
different).
Avoid using Python keywords (e.g., if, else, for) as variable
names.
Variables
assignment statement: Stores a value into a variable.
Syntax:
name = value
Examples: x = 5
gpa = 3.14
x 5 gpa 3.14
Variables
Variables are the names given to data that we need to store and
manipulate in our program.
For example ,to store the age of the user ,we can write a variable
userAge and it is defined as below.
age = 10
After we define age ,the program will allocate some memory to
store this data.
Afterwards this variable can be accessed by referring its name age.
Every time we declare a variable ,some initial value must be given
to it.
Jan 6, 2025 19
Variables
Python is dynamically typed. You do not need to declare variables.
The declaration happens automatically when you assign a value to a
variable.
Example : counter =100
name = “John”
Dynamic Typing is a technique in some languages where depending on
how a value is used, the data type of the variable is dynamically and
automatically assigned. Consider the below code in Python,
num=65 #Line 1
num="A" #Line 2
In Line 1, variable num is considered to be of type int and in Line 2, it's
Jan 6, 2025 20
a, b, c = 1, 2, "john"
Variables
Augmented Assignment :
You can combine an assignment statement and a binary
operation into a single statement.
For example :
>>> a=10
>>> a+=5
>>> a
15
+= : x+= 2 is same as equal to x=x+2
Similarly for subtraction: x-=2 is same as x=x-2
Jan 6, 2025 22
QUESTIONS
1. Which of the following is a valid variable name in Python?
Jan 6, 2025 24
Data Types in Python
Every value in Python has a datatype.
Jan 6, 2025 25
Data Types in Python
Data Types Classes Description
int, float,
Numeric holds numeric values
complex
String str holds sequence of characters
Sequence list, tuple, range holds collection of items
holds data in key-value pair
Mapping dict
form
Boolean bool holds either True or False
hold collection of unique
Set set
items
Jan 6, 2025 26
Data Types in Python
Jan 6, 2025 27
Data Types in Python
There are various data types in Python. Some of the
important types are listed below.
Numeric data types
Strings
Boolean data type
Jan 6, 2025 28
Numeric data types(Numbers)
There are three built-in data types for numbers in Python3:
Integers
Floating-point numbers
Complex numbers: <real part> + <imaginary part>j
Jan 6, 2025 29
Numeric data types
Integers can be of any length, it is only limited by the memory
available.
Jan 6, 2025 30
Numeric data types
Here are some examples.
>>> a=5
>>> type(5)
<class 'int'>
>>> x=2.5
>>> type(x)
<class 'float'>
>>> b = 2+4j
>>> type(b)
<class 'complex'>
Jan 6, 2025 31
Strings
A string is a sequence of characters.
We can use single quotes or double quotes to represent
strings.
Multi-line strings can be denoted using triple quotes( ‘‘’)
Python strings are "immutable" which means they
cannot be changed after they are created
Jan 6, 2025 32
Strings
Example :
Jan 6, 2025 33
Boolean data type
Boolean is another important type in Python.
An object of type Boolean can take on one of two
values: True or False
Notice that the value True has an uppercase "T". The
same is true for False (i.e. you must use the uppercase
"F").
Jan 6, 2025 34
Boolean data type
When you ask Python to display the type of a boolean
object it will show bool which stands for boolean:
# Type of True
>>> type(True)
<class 'bool'>
Jan 6, 2025 35
List Data Type
List : It is a comma-separated list of elements enclosed in square
brackets. These elements can be of any data type and can be
changed.
Example:
languages = [“C", "Java", "Python"]
# access element at index 0
print(languages[0])
# access element at index 2
print(languages[2])
Jan 6, 2025
Tuple Data Type
Tuple : A comma-separated list of elements in round brackets
is known as a tuple. Values can be of any data type, but they
cannot be modified.
Example:
fruit = (“apple", “orange", “banana“)
# access element at index 0
print(fruit[0]) #apple
value.
Example:
# create a dictionary named person
person = {name:"John", age :36}
print(person[name]) #prints John
Jan 6, 2025
Set Data Type
Set is an unordered collection of unique items. Set is defined by
values separated by commas inside braces { }. For example,
# create a set named student_id
student_id = {112, 114, 116, 118, 115}
# display student_id elements
print(student_id)
# display type of student_id
print(type(student_id))
Output
{112, 114, 115, 116, 118}
<class 'set'>
Jan 6, 2025
type( ) function
The type() function returns the type of the specified object.
Python refers to integers as int, floats as float, and character
strings as str.
>>> type(11)
<class 'int'>
>>> type(11.3)
<class 'float'>
>>> type("hi")
<class 'str'>
Jan 6, 2025 40
Type conversions
Type conversion is used to convert values from one
data type to another, either automatically by the Python
interpreter or manually by the programmer using in-built
functions.
Note that not all data types can be converted into each
other.
For example, there's no way a string consisting of
English letter alphabets can be converted into an integer
Jan 6, 2025 41
Type conversions
There are two types of type conversion in Python:
Jan 6, 2025 42
Type conversions
Implicit Type Conversion
In Implicit type conversion, Python automatically converts one
data type to another data type.
This process doesn't need any user involvement.
The smaller data type is converted into higher data type to
prevent any loss of data during the runtime.
Since the conversion is automatic, we do not require to use any
function explicitly in the code.
Jan 6, 2025 43
Type conversions
Implicit Type Conversion - Example
a=5
b = 5.5
sum = a + b
print (sum)
print (type (sum)) #type( ) is used to display the datatype of a
variable
Output:
10.5
<class ‘float’>
Jan 6, 2025 44
Type conversions
Explicit Type Conversion
Needs to be done manually by the programmer.
Explicit type conversion is also known as typecasting.
In Explicit Type Conversion, users convert the data type of
an object to required data type.
We use the predefined functions like int(), float(), str(), etc
to perform explicit type conversion.
Syntax :
<required_datatype>(expression)
Typecasting can be done by assigning the required data type
Jan 6, 2025
function to the expression. 45
Type Casting
Type casting helps to convert from one data type to
another .i.e from integer to a string or vice versa.
There are three built in functions in Python
int( )
float( )
str( )
Jan 6, 2025 46
Type Casting
int( ) - constructs an integer number from an integer literal, a
float literal (by rounding down to the previous whole
number), or a string literal (providing the string represents
a whole number)
>>> int("4")
4
int(“RAM”) ----- returns syntax error
int(“3.7654”) -------returns syntax error
Jan 6, 2025 48
Type Casting
>>> float(1)
1.0
>>> float("3")
3.0
>>> float("4.2")
4.2
>>> str(2)
'2'
>>> str(2.66)
'2.66'
Jan 6, 2025 49