4 Basic Elements of Python
4 Basic Elements of Python
COMMENTING IN PYTHON
• Comments in Python are preceded by a hash
symbol (#) on a line and called a line
comment.
• Three consecutive single quotation marks ‘’’
are used to give multiple comments or
comments on several lines at once and called
paragraph comment.
#Learn How to Comment in Python
print(‘I Learnt How to Comment in Python’)
‘’’ Amazing tool
in Python called Comment’’’
print(‘Bye’)
PYTHON CHARACTER SET
Python uses the following character set:
• Letters: Upper case and lower case letters
• Digits: 0,1,2,3,4,5,6,7,8,9
• Special Symbols: Underscore (_), (,), [,], {,}, +, -,
*, &, ^, %, $, #, !, Single quote(‘), Double
quotes(“), Back slash(\), Colon(:), and Semi
Colon (;)
• White Spaces: (‘\t\n\x0b\x0c\r’), Space, Tab.
Python Tokens
Keywords
Identifiers/
Variables
Tokens Operators
Delimiters
Literals
Literals
• Literals are numbers or strings or characters that
appear directly in a program.
• values used for giving inputs to the program.
• A list of some literals in Python is as follows:
78 #Integer Literal
21.98 #Floating Point Literal
‘Q’ #Character Literal
“Hello” #String Literal
True/False #Boolean Literal
• Python also contains other literals, such as lists,
tuple and dictionary.
• Display Literals in Interactive Mode
>>> ‘Hello World’
‘Hello World’
Value and Type on Literals
+ - * / // % ** Arithmetic Operator
>>> a=15
>>> b=oct(a)
>>> print(b,type(b))----0o17 <class 'str'>
>>> a=0b1111
>>> b=oct(a)
>>> print(b,type(b))--------0o17 <class 'str'>
>>> a=0xF
>>> b=oct(a)
>>> print(b,type(b))----0o17 <class 'str'>
>>> a=0XBEE
>>> b=oct(a)
>>> print(b,type(b))----0o5756 <class 'str'>
hex()
• This function is used for converting Any Base ( 2, 8, 10 ) into
Hexa Decimal Number System data (base 16)
Syntax: varname=hex(decimal / binary / Octal value)
>>> a=10
>>> b=hex(a)
>>> print(b,type(b))------------0xa <class 'str'>
>>> a=8
>>> b=hex(a)
>>> print(b,type(b))-----------0x8 <class 'str'>
>>> a=9
>>> b=hex(a)
>>> print(b,type(b))--------0x9 <class 'str'>
>>> a=0b1111
>>> b=hex(a)
>>> print(b,type(b))----------0xf <class 'str'>
>>> a=0o17
>>> b=hex(a)
>>> print(b,type(b))---------0xf <class 'str'>
float
• 'float' is one of the pre-defined class and it is
treated as fundamental data type.
• The purpose of 'float' data type is that "To store
floating Point / Real Constant values ".
• float data type allows us to store only decimal
number system values but not support to store
Binary , Octal and Hexa Decimal Number System
Values.
• float data type also supports to store “Scientific
Notation" whose general format is "Mantissa e
Exponent".
• "Mantissa e Exponent" can be converted into
general float point values as "Mantissa x 10 to
the power of Exponent“.
float
>>> a=34.99
>>> print(a,type(a))--------34.99 <class 'float'>
>>> a=0.009
>>> print(a,type(a))--------0.009 <class 'float'>
>>> a=22/7
>>> print(a,type(a))---------3.142857142857143 <class 'float'>
> a=0b1111.0b1010---------SyntaxError: invalid decimal literal
>>> a=0xF.0b1010--------SyntaxError: invalid decimal literal
>>> a=0o17.0o12---------SyntaxError: invalid decimal literal
---------------------------------------------------------------
>>> a=3e2
>>> print(a,type(a))---------300.0 <class 'float'>
>>> a=43e-2
>>> print(a,type(a))--------0.43 <class 'float'>
>>> a=0.00000000000000000000000000000000000000000004
>>> print(a,type(a))------4e-44 <class 'float'>
bool
• 'bool' is one of the pre-defined class and treated
as fundamental data type.
• The purpose of bool data type is that "To store
True / False Values (Logical Values)"
• Internally the value True is considered as 1
• Internally the value False is considered as 0
Examples:
>>> a=True
>>> print(a,type(a))----------True <class 'bool'>
>>> b=False
>>> print(b,type(b))---------False <class 'bool'>
bool
Special Examples:
>>> a=True
>>> b=True
>>> print(a+b)-------------2
>>> print(True+False)---------1
>>> print(True*False)----------0
>>> print(False+0b1111)---------15
>>> print(2*False+True)----------1
complex
• 'complex' is one of the pre-defined class and treated as
fundamental data type.
• The purpose of complex data type is that "to store complex
values".
• The General Format of complex value is:
a+bj (or) a-bj
here 'a' is called Real Part
here 'b' is called Imaginary Part
and 'j' is called sqrt(-1)
• To extract the real part we use a pre-defined attribute
called "real" present complex object
• To extract the imaginary part we use a pre-defined attribute
called "imag" present complex object
Syntax:- complexobj.real ---->Gives real part
complexobj.imag--->Gives imaginary part
Internally real and imaginary parts are treated as float.
complex
>>> a=2+3j
>>> print(a,type(a))------------(2+3j) <class 'complex'>
>>> b=2.3+4.6j
>>> print(b,type(b))------------(2.3+4.6j) <class 'complex'>
>>> c=2-3j
>>> print(c,type(c))------------(2-3j) <class 'complex'>
>>> d=-2.5-3.5j
>>> print(d,type(d))----------(-2.5-3.5j) <class 'complex'>
>>> e=23+4.5j
>>> print(e,type(e))-----------(23+4.5j) <class 'complex'>
>>> x=2+3i---------SyntaxError: invalid decimal literal
>>> a=2+j3-------NameError: name 'j3' is not defined
complex
>> a=2+3j
>>> print(a,type(a))---------(2+3j) <class 'complex'>
>>> a.real---------2.0
>>> a.imag-----3.0
>>> b=2.3+4.5j
>>> print(b.real)--------2.3
>>> print(b.imag)-----4.5
>>> print((2+3.4j).real)---------2.0
>>> print((2+3.4j).imag)--------3.4
String Type
• Strings in Python are arrays of bytes representing
unicode characters.
• However, Python does not have a character data
type, a single character is simply a string with a
length of 1.
• A string literal or string in Python can be created
using single, double and triple quotes.
>>> D = ‘Hello World’
>>> D
‘Hello World’
>>> D=”Good Bye”
>>> D
‘Good Bye’
String Type
• The triple single quotes are used to write a
multiline string.
>>> Sentence=‘’’Hello, How are you? Welcome to
the world of Python Programming. It is just the
beginning. Let us move on to the next topic.’’’
>>> Sentence
‘Hello, How are you? Welcome to the world of
Python Programming. It is just the beginning. Let us
move on to the next topic.’
The str Function
• The str function is used to convert a number
into a string. The following example illustrates
the same.
>>> 12.5 #Floating Point Number
12.5
>>> type(12.5)
<class ‘float’>
>>> str(12.5) #Convert floating point number to string
‘12.5’
The String Concatenation (+) Operator
print(format(10.345,”10.2f”)) 10.35
print(format(10,”10.2f”)) 10.00
print(format(10.32245,”10.2f”)) 10.32
print(format(0.31456,”10.2%”)) 31.46%
Justifying Format
print(format(10.234566,”10.2f”)) #Right Justification Example 10.23
print(format(10.234566,” <10.2f”)) #Right Justification Example 10.23
Formatting String
print(format(“Hello World!”,”25s”) #Left Justification Example Hello World!
print(format(“HELLO WORLD!”,”>20s”)) #String Right Justification HELLO WORLD!
Formatting Scientific Notation
PYTHON INBUILT FUNCTIONS
The ord and chr Functions
• ord(‘A’) #Returns ASCII value of Character
‘A’
• chr(Code) returns the character
corresponding to its code
import random
print(random.randrange(1, 10))