0% found this document useful (0 votes)
7 views13 pages

Python Character Set and Tokens Explained

The document outlines the Python character set, which includes letters, digits, special symbols, and whitespace, and explains the concept of tokens, including keywords, identifiers, literals, and operators. It details various types of literals, such as string, numeric, boolean, and collections, as well as different operators like arithmetic, assignment, relational, logical, and membership operators. Additionally, it covers variables, dynamic typing, comments, user input through the input() function, and the print() function's features.

Uploaded by

Shiv Kumar
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)
7 views13 pages

Python Character Set and Tokens Explained

The document outlines the Python character set, which includes letters, digits, special symbols, and whitespace, and explains the concept of tokens, including keywords, identifiers, literals, and operators. It details various types of literals, such as string, numeric, boolean, and collections, as well as different operators like arithmetic, assignment, relational, logical, and membership operators. Additionally, it covers variables, dynamic typing, comments, user input through the input() function, and the print() function's features.

Uploaded by

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

Python Character Set : is a set of characters that Python language

recognises. A character can be any letter,digit or any other symbol .


Python supports Unicode encoding standard.

Python has the following character set :

a) Letters : A-Z , a-z


b) Digits : 0-9
c) Special symbols : ! @ # $ ^ & * ( _ + = % etc.
d) Whitespaces : Blank space ,tabs , newline etc.
TOKEN : The smallest individual unit in a program is known as token.
Python has following tokens :
a) Keywords b) Identifiers c) Literals d) Operators

KEYWORDS : Keywords are set of special words which are reserved by


python and have specific meanings .
For example : for,if, True,False,elif etc.

IDENTIFIERS: They are user defined names to represent programmable


entity like variables,functions,classes or any other objects.
There are some rules for identifiers :
a) First character must be alphabet , digits can be used, no special symbols
except underscore.
b) Keywords cannot be used as identifiers.
LITERALS/VALUES : are data items that have a fixed [Link] has
following literals
Literals

String Numeric Boolean None Collections

String Literals : The text enclosed in single or double quotes.


For example ‘abc12’ , “@qwe” are string literals

Numeric Literals : to represent numbers. The numeric types are int(used


for numbers without fractional part), float( for numbers having integral and
fractional part) and complex( for complex numbers)
Boolean Literal : is used to represent one of the two Boolean values
i.e. True or False.
None Literal : is used to indicate absence of value.

Collections : such as Lists and Tuples.

OPERATORS : are tokens when applied to operands (variables


and constants) generate value .
For example : a+b
Here a and b are operands , + is operator . The + operator when
applied provides the sum of values of a and b.
The whole process is called operation and its name is addition.
ARITHMETIC OPERATORS : 5 + 4 evaluates to 9
5-4 evaluates to 1
5*4 evaluates to 20
5**4 i.e 5 to power 4 =625
5/4 evaluates to 1.25
5%4= 1(rem when 5 is divided by 4)
25//4=6(floor division)

ASSIGNMENT OPERATORS: = ( It assigns the value computed in


RHS to the variable in LHS.)

A=2 Here 2 is assigned to variable A


And in next step 2 is added to 2
and result i.e 4 is assigned to B
RELATIONAL OPEARTORS : determine the relation among different
operands. a=5 , b=10 c=100 ,d=1
(i) < (less than ) a<b evaluates to True
c<b evaluates to False
(ii) <=(less than or equal to) a<=5 evaluates to True
c<=5 evaluates to False
(iii) > (greater than ) c>b evaluates to True
d>b evaluates to False
(iv) >=(greater than or equal to) c>=5 evaluates to True
d>=5 evaluates to False
(v)==(equal to) a==5 evaluates to True
b==5 evaluates to False
(vi) >=(not equal to) c!=5 evaluates to True
a != 5 evaluates to False
LOGICAL OPERATORS : Python provides three logical operators to
combine existing expressions.
The logical operators are : and , or , not
a) and : The and operator evaluates to True if both of its operands
evaluate to True ; False if either or both operands evaluate to false.
(15==20) and (100>10) evaluates to False.
(20==20) and (100>10) evaluates to True.
b) or : The or operator evaluates to True if either of its operands
evaluate to True ; False if both operands evaluate to false.
(15==20) or (100>10) evaluates to True.
(20<10) or (1>10) evaluates to False.
c) not : The not operator reverses the truth value of expression.
not(10==10) evaluates to false because 10==10 is true
not(5==10) evaluates to true because 5==10 is false
MEMBERSHIP OPERATORS are used to test if sequence is presented in
an object.
in : returns True if a sequence with the specified value is present in the
object.
not in : returns True if a sequence with the specified value is not present in
the object.

x=["ARAVALI", "NILGIRI ", “SHIVALIK"]


print("NILGIRI " in x)
# returns True because a sequence with the value "NILGIRI " is in the list.

y=["ab", "ba"]
print("ab" not in x)
# returns False because a sequence with the value "ab" is in the list.
VARIABLE : A variable in Python represents named location that refers to a
value and whose values can be used and processed during program run.

For example x=10 will create a variable x of numeric type.


y="SANJAY" will create a variable y of String type

In Python , a variable is not created untill some value is assigned to it.

Different ways of assigning a value to a variable :


a) Assigning same values to different variables :
a,b,c,d=0 It will assign 0 to all the variables i.e. a,b,c,d
b) Assigning different values to different variables :
a,b,c,d=2,3,4,5 It will assign 2,3,4,5 to a,b,c,d respectively
a,b=c,d It will assign value of c to a and d to b respectively
Dynamic Typing : A variable pointing to a value of a certain type ,
can be made to point to a value/object of different type
x=3.2
x= "python"
In the first statement x is pointing to a float value and in second statement
it x is pointing to a string value.

Comments : are written to help the programmer understand the source


code. It makes the code more readable . Comments are ignored by the
Python interpreter.
a) Single line comment : It begin with a # symbol.
# Comments make the program more readable.
b) Multi line comment : written within triple quotes(' ' ' ) or (" " ")
' ' ' It makes the code more readable . Comments are ignored by the Python
interpreter. ' ' '
Input using the input() function : Python has an input function
which lets you ask user for some text input. You call this function
to tell the program to stop and wait for the user to key in the data.
User types in admission
>>> x=input("Enter admission number =") number
Enter admission number =1234
User types in age
>>> y=input("Enter your age = ")
Enter your age = 12
>>> x
'1234'
>>> y
'12'
Note : input() function always returns a value of String type
>>> x=input("enter any number =") Error:
enter any number =12 Traceback (most recent call last):
>>> x+1 File "<pyshell#1>", line 1, in
Python offers two functions int() <module> x+1
and float() to convert the values TypeError: can only concatenate str
received through input() into int (not "int") to str
and float
types

>>> x=input("enter any number =") >>> x=input("Enter any number = ")
enter any number =12 Enter any number = 12.35
>>> x=int(x) >>> x=float(x)
>>> x >>> x
12 12.35
print() :The print() function prints the specified message to the screen.
Features of print statement :
Syntax : print(object(s),sep=separator,end=end)
a) It auto converts the items to strings
b) It inserts spaces between items automatically because the value of sep
argument is space(‘ ‘).
c) It appends a new line character at end of new line unless u give your end
argument.
x=int(input("Enter your Age = "))
print(" Your age is",x) Enter your Age = 12
print(" Your age is",x, x,sep=".....") Your age is 12
print(" PYTHON IS") Your age is.....12.....12
print(" A PROGRAMMING LANGUAGE ") PYTHON IS
print(" PYTHON IS" , end = " -> ") A PROGRAMMING LANGUAGE
print(" A PROGRAMMING LANGUAGE ") PYTHON IS -> A PROGRAMMING LANGUAGE

You might also like