Tokens in Python
A program in Python contains a sequence of
instructions.
Python is a case sensitive language (age and Age are
different)
Python breaks each statement into a sequence of
lexical components known as tokens.
List of tokens supported by python are as follows
Tokens Meaning and Examples
a) Keywords - Reserved words (ex: if, else, for)
b) Identifiers - Name used to define/find variable
c) Operators - Used to perform operations.(<,>,+,* etc)
d) Delimiters - Symbols are delimiters.( :, ‘,’, ;, etc)
e) Literals -Can be anything number of string( 78,
’21.90’,’bye’)
Python Core Types (Data Types)
Integer - 10, 20, -2, etc…
Float - 12.34, 45.21, 12.766 , etc.
Complex – 1 + 4j, 2-8j
Boolean – True, False
String - ‘Hello World’, ‘bye’
Doesn't have char data type
Note: In Build function type is used to know the type of any
value.
Example: (Execute on interactive mode)
>>> type(‘Hello World’)
<class ‘str’>
>>> type(123)
<class ‘int’>
>>> type(3+4j)
<class ‘complex’>
The print() Function
The print() function is used to display the contents on the
screen.
Syntax:
print(arguments)
Note: The argument of the print function can be a value of any
type int, str, float etc.
Example:
>>>print('Welcome to Python')
Welcome to Python
>>> a = 10
>>> print(a)
10
Assigning Values to a Variable
Assigning value to a variable is called an assignment
statement.
Syntax:
Variable = Expression
Example:
>>> a = 10
>>> a
>>> 10
>>> str = ‘Hello’
>>> str
>>> ‘Hello’
Multiple Assignments
Python supports simultaneous assignment to
multiple variables.
Syntax:
Var1,Var2, Var3…. = Exp1, Exp2, Exp3…
>>> P , Q = 10, 20
>>> p
10
>>> Q
>>> 20
The input() function
It is used to accept an input from a user. Note
that the blinking cursor waits for the user's
input. The user enters his input and then hits
Enter. This will be captured as a string.
Syntax:
Variable_Name = input()
OR
Variable_Name = input(‘String’)
Example:
>>> str1 = input('Please Enter the String:')
Please Enter the String:amit
>>> print(str1)
amit
Comments in Python
In a Python script, the symbol # indicates the start of a
comment line.
It is effective till the end of the line in the editor. If # is the
first character of the line, then the entire line is a
comment. It can be used also in the middle of a line. The
text before it is a valid Python expression, while the text
following is treated as a comment.
Single-line comments are created simply by beginning a
line with the hash (#) character, and they are automatically
terminated by the end of line.
Comments in Python
FORMATTING NUMBER AND
STRINGS
A formatting string helps make string look presentable to the user for
printing.
A programmer can make use of format function to return a formatted
string.
Syntax:
format(item, format-specifier)
Where,
- item is the number or string
- format-specifier is a string that specifies how the item is
formatted
Example:
>>> z= 12.345789
>>> z
12.345789
>>> format(z,'.2f')
'12.35'
Python Various inbuilt function
Name of Function Meaning Example:
max(x1,x2, x3,…….XN) Returns Largest >>>max(10,20,30)
among X1, X2, >>>30
…..XN
min(x1,x2, x3,…….XN) Returns smallest >>>min(10,20,30)
among X1, X2, >>>30
…..XN
abs(x) Returns absolute >>>abs(-10)
value of x >>>10
>>>abs(4)
4
pow(X,Y) Returns XY >>>pow(2,3)
8
Round(x) Return the value >>> round(10.23)
nearest to the 10
value of x >>> round(10.90)
11
List of Function under math Module
Name of Meaning Example:
Function
Round X to nearest integer and >>>import math
ceil(X) returns that integer. >>> math.ceil(10.23)
11
floor(X) Returns the largest value not greater >>>import math
than X >>> math.floor(18.9)
18
exp(X) Returns the exponential value for ex >>>import math
>>> math.exp(1)
2.718281828459045
log(X) Returns the natural logarithmic of x >>>import math
(to base e) >>> math.log(2.71828)
0.999999327347282
log(x,base) Returns the logarithmic of x to the >>>import math
given base >>> math.log(8,2)
3.0
Note: a) There are many other function such as sqrt(), sin(), cos(), tan(),
asin(), acos(), degrees(), etc under math module
b) The statement import math should be the first statement should be written on
interactive mode when you are trying to execute any of the math function.
Conclusion
keywords, Identifiers, Operators, Delimiters, and
Literals are the various list tokens supported by
python.
print function is used to display contents on the screen
The input() function is used to accept input from the
user.
format() function can be used to return a formatted
string.