0% found this document useful (0 votes)
673 views49 pages

Tokens in PYTHON

Uploaded by

maha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
673 views49 pages

Tokens in PYTHON

Uploaded by

maha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Character set

 Character set is the set of valid characters that a language


can recognize.
 A character represents any letter, digit or any other symbol.
 Python has the following character sets:
 Letters – A to Z, a to z
 Digits – 0 to 9
 Special Symbols - + - * / { } & $ etc.
 Whitespaces – Blank Space, tab, carriage return, newline
 Other characters –All ASCII(American Standard Code for
Information Interchange) and UNICODE characters are
supported by Python that constitutes the Python character set.

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.

 They can’t be used as variable names, function names, or any


other random purpose.

 Any name can be given to a variable however, we cannot use


some of the built-in keywords of the language.

 These keywords are known as reserved words.

Jan 6, 2025 3
Keywords (Reserved words)

 you cannot use them as constant or variable or any other


identifier names.
 All the Python keywords contain lowercase letters only.

Examples :
for print continue break
def if return del
import elif in while
else is and not
Python Identifiers

 A Python identifier is a name used to identify a variable,


function, class, module or other object.
 An identifier starts with a letter A to Z or a to z or an
underscore ( _ ) followed by zero or more letters, underscores
and digits (0 to 9).
 Python is a case-sensitive programming language.
Thus, Manpower ( with a capital M) and manpower (with a
small m) are different identifiers.
Identifier Naming Rules

1. An identifier should start with a letter A to Z or a to z or


an underscore (_) followed by zero or more letters,
underscores, or digits (0 to 9).
Examples of a valid identifier:
num1
FLAG
get_user_name
userDetails
_1234

2. An identifier cannot start with a digit. If we create an


identifier that starts with a digit then we will get a syntax
error.
Identifier Naming Rules
3. We also cannot use special symbols in the identifiers
name.
Symbols like ( !, @, #, $, %, . ) are invalid.

4. A keyword cannot be used as an identifier. In Python,


keywords are the reserved names that are built-in in
Python. They have a special meaning and we cannot use
them as identifier names.
Literals
 Literals (constants) are data items that have a fixed value.
 The constant values or data objects utilized in a source program are
known as literals.
 Python allows several kinds of literals:
(i) String literals
(ii) Numeric literals
(iii) Boolean literals
(iv) Special Literal
(v) Literals Collections
Literals
 String Literals:
 The text written in single, double, or triple quotes represents the
string literals in Python. For example: “Computer Science”, ‘sam’,
etc.
 We can also use triple quotes to write multi-line strings.
# Python program to show how to create string literals

# Examples of string literals


a = 'Hello’
b = "World"
c = "'Python is a
high-level and
general purpose language'"

print(a)
print(b)
print(c)
Literals
Output:

Hello
World
Python is a
high-level and
general purpose language

Character Literals: Another string literal is called the character literal,


which encloses the letter in single or double quotes.
Literals
 Numeric Literals: These are the literals written in form of
numbers.
Python supports the following numerical literals:
 Integer Literal: It includes both positive and negative numbers

along with 0. It doesn’t include fractional parts. It can also


include binary, decimal, octal, hexadecimal literal.
 Float Literal: It includes both positive and negative real

numbers. It also includes fractional parts.


 Complex Literal: It includes a+bj numeral, here a represents

the real part and b represents the complex part.


Literals
 Boolean Literals:
 Boolean literals have only two values in Python.
 These are True and False.

 Special Literal: Python has a special literal ‘None’. It is used to


denote nothing, no values, or the absence of value.

# Examples of the special literal


var = None
print(var)

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.

 Punctuators are symbols that are used to structure programming


sentences in a computer language.

 Some of the Punctuators are: [ ] ,{ }, ( ), @ ,#, : , = , -= etc.,


Creating and using variables in Python

 A variable is essentially a name that is assigned to a value.


 Unlike other programming languages, Python has no command
for declaring a variable.
 A variable is created the moment you first assign a value to it.
 Need to assign (initialize)
 use of uninitialized variable raises exception

 The equal sign (=) is used to assign values to variables.


 The operand to the left of the = operator is the name of the
variable and the operand to the right of the = operator is the
value stored in the variable.
Rules for Naming Variables

To use variables effectively, we must follow Python’s naming


rules:
 Variable names can only contain letters, digits and

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

type is reassigned to String.


Variables
 Variables can change type, simply by assigning them a new
value of a different type.
Example :
x =1
x = “hello”
 Python allows you to assign a single value to several variables
simultaneously.
a = b = c =2
 You can also assign values to multiple variables at the same
time.(Multiple assignment) :
Jan 6, 2025 21

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?

(i) 1st_string (ii) while (iii) my_string_1


3. What are the rules for writing an identifier?
4. What is type conversion? How is it done in Python?
5 a) Which of the following is a valid variable name in Python?
(i) 12xyz (ii) break (iii) A_123 (iv) A?B?C
b) Evaluate the expression x**y**z given x=2,y=3,z=2 .
c)Define keyword and enumerate some of the keywords in
Python.
Jan 6, 2025 23
QUESTIONS

1. Explain the basic data types available in Python, with


examples.
2. Describe numeric data types and character sets in python with
examples.
3. With examples, define keywords, variables, and assignment
statements.

Jan 6, 2025 24
Data Types in Python
 Every value in Python has a datatype.

 Data type specifies what type of data will be stored in


variables.

 Since everything is an object in Python programming, data


types are actually classes and variables are instance
(object) of these classes.

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

 They are defined as int, float and complex classes in Python.


 We can use the type() function to know which class a variable
or a value belongs to.

Jan 6, 2025 29
Numeric data types
 Integers can be of any length, it is only limited by the memory
available.

 A floating-point number is accurate up to 15 decimal places.


Integer and floating points are separated by decimal points.
 1 is an integer, 1.0 is a floating-point number.

 Complex numbers are written in the form, x + yj, where x is


the real part and y is the imaginary part.

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 :

>>> my_string = 'Hello'


>>> print(my_string)
Hello
>>> type(my_string)
<class 'str'>

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

# access element at index 1


print(fruit[1]) #orange
Jan 6, 2025
Dictionary Data Type
 Python dictionary is an ordered collection of items.
 It stores elements in key/value pairs.
 Here, keys are unique identifiers that are associated with each

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:

 Implicit Type Conversion


 Done automatically by the Python interpreter.
 Explicit Type Conversion
 Needs to be done manually by the programmer.

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)

 float ( ) - constructs a float number from an integer literal, a


float literal or a string literal (providing the string represents
a float or an integer)

 str( ) - constructs a string from a wide variety of data types,


including strings, integer literals and float literals
Jan 6, 2025 47
Type Casting
 To change a float to an integer type
>>> int(7.3256)
7
>>> int(7.65)
7
 To change a string into integer

>>> 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

You might also like