PYTHON BASICS
TOKENS
Tokens are the smallest individual unit in a python program. Tokens are also
called lexical unit.
There are 5 units in tokens,
Keywords
Identifiers
Literals
Punctuators
Operators
Keywords
Keywords are reserved words and it has special meaning to python interpreter.
e.g., None, True, False, if, else, for, while, elif, in, not in, break, continue, pass,
global, non local, lambda, is, try, except, finally, import, as, def, class, raise,
from, del, assert, yield, return, with, and, or
Identifiers
Identifiers are names given to variable, class, object, function, list, tuple,
dictionary and so forth.
Rules for identifiers
Variable name must be made up of only letters, numbers and
underscore.
Variable name must only be a non-keyword with no space in-between.
Variable name cannot begin with a number although they can contain
number.
e.g.,
valid identifiers invalid identifiers
File 1file
File1 Add num
Account_No Acc-no
WHILE while
FOR For
literals
Literals are data items that have a fixed /constant value.
Types of literals :
1. String literal :
single line strings
Multiline strings
2. Numeric literal
Int, float, complex
Integer literal :
decimal literal, octal literal, hexadecimal literal
Floating point literal
Complex number literal
3. Boolean Literal
True, False
4. Special Literal
None
Operators
Operators is a token unit that trigger some computation/action when
applied to variables and other objects in an expression.
Arithmetic operators +,-,*,/,%,**,//
Bitwise operator &,^,!
Shift operator >>,<<
Identity operator is ,is not
Relational operator >,<,==.!=,<=,>=
Logical operator and,or,not
Assingment operator =
Membership operator in , not in
Agumented assignment +=,-=,*=,/=,%=,/
Operator precedence: (Arithmetic)
() Parenthesis
** Exponentiation
/,//,%,* Divide, floor division, Modulus,
Multiply
+,_ Addition , Subtraction
Operator precedence (logical operator)
Not Revert
And Multiply
Or Addition
Data types :
Integer :
Float
Complex number
Boolean
Type casting
Explicit (Forced) conversion :
Explicit conversion also called type casting happens when data type
conversions takes place deliberately i.e., the programmer forces it in
the program.
Example :
>>>X=10.0
>>>Print(int(X))
>>>10
Implicit conversion :
Implicit conversion, also known as coercion ,happens when data type
conversion is done automatically during run-time by python and is not
instructed by the programmer.
Example :
>>>n=10
>>>print(n)
>>>print(type(n))
10
<class ‘int’>
Jump statements :
break ,continue, pass
break :
the break statement skips the rest of the loop and jump over to the
statement following the loop. A break statement terminates the very
loop it lies within execution resumes at the statement immediately
following the body of terminates statement.
continue :
the continue statement skips the rest of loop statement and causes the
next iteration of the loop take place .
Note:
unlike break statement ,the forces the next iteration of the loop to take
place. Skipping any code in between.
Conditional statement Syntax
Simple if statement if <condition>:
statement
if -else statement if <condition>:
Statement
else :
statement
if-elif-else statement if <condition>:
statement
elif <condition>:
statement
else :
statement
for loop for< variable> <in sequence/items in
range> :
<statement in body of loop>
range range(start,stop,skip)
While while <logical expression>:
loop body