Professional Documents
Culture Documents
if i%2==0:
print(i)
TOKENS IN PYTHON:-
1. Keywords:-for, in, if
2. Identifiers:-i
3. Literals:-1, 11, 2, 0
4. Operators:-%, ==
5. Punctuators:-, and :
Identifiers
An identifier is a name given to entities like classes, functions, variables,
objects, lists, dictionaries etc. It helps to differentiate one entity from
another.
Literals
Literals are data items that have a fixed value.
Several kinds of literals: - (like String, Numeric, Special and Boolean)
String Literals
String literals: - A string literal is a sequence of characters surrounded by
quotes. We can use single, double and triple quotes for a string.
Some valid string literals are „abc‟, “abc”, „‟‟abc‟‟‟, „123‟, „1-x-0‟, „a‟,
“a” etc.
print(type(„123‟)), print(type(„a‟)
Single line strings: - The string that you create by enclosing text in single
quotes or double quotes in one line is known as single line string.
print(type('python')) print(type("python")) print(type('1-a-0'))
Multiline strings: - The string that you need to write multiple lines is known
as multiline string.
a = 'Hello\ a = “Hello\ a = „„„Hello
world' world” world‟‟‟
print(a) print(a) print(a)
Note: - Don‟t forget to use backslash in single or double quotes.
Here backslash is used to continue the text. It is also known as EOL(End Of
Line).
Numeric Literals
Numeric Literals: - Numeric Literals are immutable (unchangeable). Numeric
literals can belong to 3 different numerical types: Integer, Float and Complex.
Integer Literal: - An integer constant must have at least one digit and must
not contain any decimal point. It may either +ve or -ve sign.
print(bin(65)) # 0b1000001 Binary Literals
print(hex(65)) # 0x41 Hexadecimal Literal
print(oct(65)) # 0o101 Octal Literal
print(ord('A')) # 65 Decimal Literal
print(chr(65)) # A String Literal
print(str(0b1000001)) # 65 Decimal Literal
print(str(0x41)) # 65 Decimal Literal
print(str(0o101)) # 65 Decimal Literal
print(int('0b1000001',2)) # 65 Decimal Literal
print(int('0x41',16)) # 65 Decimal Literal
print(int('0o101',8)) # 65 Decimal Literal
Float Literal: - A real constant in fractional form must have at least one
digit with the decimal point. It may also either +ve or -ve sign.
print(float(65)) # 65.0 fractional form
A real constant in exponent form has two parts: one is mantissa and
another is an exponent. The mantissa is followed by a letter E or e and the
exponent.
print(pow(.3,100)) # 5.153775207320094e-53
print(.3 ** 100) # 5.153775207320094e-53
Complex Literal: Python represents complex numbers in the form A + Bj. Here j
represents imaginary number. The meaning of j is √-1. a = (1+2.56j) +
(1+2.56j)
print(a) # (2+5.12j) print(a.real) # 2.0 print(a.imag) # 5.12
print(complex(65)) # (65+j)
print(complex(65, 2)) # (65+2j)
Special Literal: - Python has one special literal which is used to indicate
absence of value. The name of special literal is None.
def myFunction():
a=1
b=2
c=a+b
#return (c)
x=myFunction() # None
print(x)
#After finding the calculation, this function will be returned the value.
def myFunction(): #myFunction() is the name of function.
a=10 #the value of a is assigned 10.
b=20 #the value of b is assigned 20.
c=a+b #the calculation of a and b is stored in c.
return (c) #the value will be returned here.
x=myFunction() #the function will be called here.
print(x) #this statement will be printed the value of c.
INLINE COMMENT:-The line starts in the middle of a physical line after python
code is called an inline comment.
DOCSTRINGS:-Comments enclosed in a triple quotes(""") or triple apostrophe(''')
are called docstrings or multiline comment.
def myFunction():
a=int(input("Enter the first value:"))
b=int(input("Enter the second value:"))
c=a+b
return(c)
x=myFunction()
print(x)