0% found this document useful (0 votes)
19 views31 pages

Python Tokens: Keywords, Identifiers, Operators

The document provides an overview of tokens in Python, including keywords, identifiers, literals, operators, and punctuators. It explains the definitions and rules for each token type, along with examples of valid and invalid identifiers, as well as different types of literals. Additionally, it covers variable assignments, control flow structures, and decision-making statements in Python programming.

Uploaded by

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

Python Tokens: Keywords, Identifiers, Operators

The document provides an overview of tokens in Python, including keywords, identifiers, literals, operators, and punctuators. It explains the definitions and rules for each token type, along with examples of valid and invalid identifiers, as well as different types of literals. Additionally, it covers variable assignments, control flow structures, and decision-making statements in Python programming.

Uploaded by

Sree Ency
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Advance Python Programming

Grade 10
Tokens in Python
► The smallest unit/element in the python
program/script is known as a Token or a Lexical
unit.
► Python has following Tokens:
► Keywords
► Identifiers
► Literals
► Operators
► Punctuators
Keywords
Keywords are the reserve words/pre-defined words/special words
of python which have a special meaning for the interpreter.
False True None def if

lambda class yield continue else


assert or while break elif

del from is not pass

for global finally import as

in nonlocal return With And


int except Raise print
Identifiers(user defined names)
► Identifiers are the name given to the different
programming elements like variables, objects,
classes, functions, lists, dictionaries etc.

Non- • Must be • Cannot


keyword made up of begin
word only letters, with a
with no numbers Number,
Rule 2

Rule 3
Rule 1

space in and although


between Underscore(_) may
contain
numbers
\

Identifiers
Some Valid Identifiers:
Identifiers
Some Invalid Identifiers:

VID-REC
• Contains special character -(hyphen)

2020RNO
• Starting with a Digit

continue
• Reserved keyword

[Link]
• Contains special character . (dot)
Literals
► Literals are the data items that have a fixed or
constant value
String Literals
► A string literal is a sequence of characters surrounded
by Quotes (Single, Double or Triple Quotes).

Single Line Strings

• string that
terminate
in one line

eg- ”I am a student”, ”hi friends”


MultiLine Strings
the strings that are spread across multiple lines .These strings are created in
foloowing two ways:

[Link] adding a backslash at the end of physical line


eg-
txt_1=“hello\
world!’

[Link] enclosing the multiple lines of string in triple quotes

eg-

text_2=“”hello world!
welcome!!
to the world of python.””
String Literals

Single line String


>>>Txt1 = “Hello World”

Multiline string
>>>Txt2 =
“Hello\ World”
>>>Txt3 =‘’’Hello World”’

Escape sequences – Python allows


Numeric Literals
► Numeric Literals are numeric values like integer
floating point number or a complex number

float long

• long integers or
longs are integers
of unlimited size
Numeric Literals
► integer literals

Decimal Octal Form Hexadecimal


Form form

• Digits 0-9 • Begin with Oo • Begin with Ox


• Base 10 • Base 8(0-7) • Base 16(0-9,a-f)
• e.g. 1234 • e.g. 0o32 • [Link]
Numeric Literals
► Floating Point Literals/ Real Literals & Complex Numbers
n

Floating point Complex Number

• Decimal point • a+bj form


divides the • a & b are real
integer and fraction • j = v-1, imaginary
part
• e.g. 2+3j
• e.g. -13.0, 3.E2,
0.17E5
Boolean Literals
► A Boolean literal in Python is used to represents
one of the two Boolean Values i.e. True or False

Boolean Boolean
True False
Special Literal – None(empty legal value)

In python, None literal is used to indicate


something that has not yet been
created /absence of value
Operators in Python
► Operators are the symbols or words that perform some kind
of operation on given values (operands) in an expression and
returns the result.
arithmetic • +,-,/,*,%,**,//
bitwise • &, ^, |

identity • is, is not


relational •>,<,>=,<=,==,!=

logical • and, or,not


shift • <<, >>
Assignment •=

Membership • in, not in


+=,-=,//=,**=,*=,/=
arithmetic-assignment
Punctuators in Python
► Punctuators are the symbols that are used in
programming language to organize sentence structure,
indicate the rhythm and emphasis of expressions,
statements and Program Structure.
► Common Punctuators are:

‘ ‘’ “” # \ (){}[] @,
, : .;
Tokens in Python
Keywords are the reserve words of python which have a special meaning for
the interpreter.

Identifiers are the name given to the different programming elements like
variables, objects, classes, functions, lists, dictionaries etc.

Literals are the data items that have a fixed /constant value.

Operators are the symbols or words that perform some kind of operation on
given values (operands) in an expression and returns the result.

Punctuators are the symbols that are used in programming language


to organize sentence structure, indicate the rhythm and emphasis of
expressions, statements and Program Structure.
variables and assignments
variables represent named storage locations,whose values can be
manipulated during program run.

Variables are containers for storing data values

Creating Variables

➢ Python has no command for declaring a variable.


➢ A variable is created the moment you first assign a value to it.
x=5
y = "John"
print(x)
Link to practice
print(y)
Multiple assignments
python is very versatile with assignments

i)Assigning same value to multiple variables:


Python allow you to assign the same value to x = y = z = "Orange"
multiple variables in one line Link to practice print(x)
print(y)
print(z)
ii)Assigning multiple values to multiple variables
Python allows you to assign values to multiple variables in one line:

x, y, z = "Orange", "Banana", "Cherry"


print(x)
print(y)
print(z)
Programs
[Link] a program to input your name and print it
The input() function allows user input.

[Link] a program to calculate the area of a rectangle


Write a program to obtain three numbers and print
their sum

Challenge: Write a program to input a number and print its cube


Flow of control

The order of execution of the statements in a program is known as flow of control


Two main types of control structures in Python:
Selection (decision making)
Repetition (loops)
Program to print the difference of two numbers.
6.2 Selection

Decision making statement used to control the flow of execution


of program depending upon condition.
There are three types of decision making statement.
1. if statements
2. if-else statements
3. Nested if-else statement
1. if statements

The syntax of if statement is:


if condition:
statement(s)
oIf the condition is true, then the indented statement(s)
are executed
oThe indentation implies that its execution is dependent
on the condition.
oThere is no limit on the number of statements that can
appear as a block under the if statement.
[Link] ..else statements
if..else statement allows us to write two alternative paths and the control condition
determines which path gets executed.
The syntax for if..else statement is as follows.
if condition:
statement(s)
else:
statement(s)
[Link] a program in python to check that entered
number is even or odd
[Link] a program to find the biggest among two
numbers
Simple calculator Task: Write a program that takes two numbers and an
operator (+, -, *, /) from the user and performs the corresponding operation.
Print the result or an error message if the operator is invalid.
WAP to check whether the number is positive ,
negative or zero

num = float(input("Enter a number: "))


if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")

You might also like