Python Fundamentals
Python Fundamentals
Python
Fundamentals
Letters : A – Z, a – z
Digits :0–9
Special Symbols : + - * / ^ () : etc.
White spaces : space, tab, newline etc
Other characters : any of the 256 ASCII and Unicode
characters
1
03-08-2022
Tokens
The smallest individual unit in a program is known as a
Token or a lexical unit.
Python has following types of tokens:
• Keywords
• Identifiers (Names)
• Literals
• Operators
• Punctuators
Keywords
Keywords are the words having a special meaning
reserved by the programming language.
2
03-08-2022
Identifiers
Identifiers are fundamental building blocks of a program and are used as the
names given to different parts of a program e.g. variable, object, classes,
functions etc.
Rules for forming identifiers:
Literals
Literals/constants are data items that never changes their values
during execution of program or have a fixed value.
3
03-08-2022
String Literals
A string literal is a sequence of characters surrounded by quotes
(single/ double/triple).
e.g. 'Welcome', ''Computer Science''
String Types
Two types in Python:
i) Single line String ii) Multiline String
>>>Text = '''abc
a) Can be created as:
def
>>>Text = 'Python program'
pqr'''
>>>Text = ''Python program''
>>>abc
>>>Text
def
Python program pqr
len(Text)=11
b) Can be created as: ''' can be used for writing
>>>Text = 'abc\ multiline string it
def\ treats/counts newline as a
pqr' single character
>>>Text
abcdefpqr
len(Text) = 9
\continuation of string
4
03-08-2022
Numeric Literals
Can be of three types:
• Integer
Ex. 235, -45,
0o457, 0O306 (Octal , 0-zero, o-letter)
0x69A, 0X5B8 (Hexadecimal, 0-zero, x-letter)
0b1011, 0B1011 (Binary, 0-zero, b-letter)
• float
Ex. 17.5, .8, 179E05, 3.E3, 25E–4, 4.E5
• complex
of the form a+bj where j (or J) =
5
03-08-2022
Operators
Operators are tokens that perform some computation when
applied to variables and other objects in an expression.
Operators can be –
• Unary(requires one operand)
e.g. – (unary minus), +(unary plus)
~(bitwise complement)
not (logical negation)
Punctuators
Punctuators are the symbols used in programming languages to
organize statements
6
03-08-2022
Sample Program
# Sample program Comment in the program,
# Definition of function Print begins with #
def Print():
User defined function
print ("Function print")
# Main program code
A = 15
Expressions
B = A-10
print (A+3)
if B>10 : : specifies beginning of a block ,
block begins with an indentation
print("Value more than 10") and can contain more than one line
else: #block
print("Value less than 10")
print()
Components of a Program
It can be:
• Statements
• Comments
• Functions
• Expressions
• Blocks
7
03-08-2022
Statements
Smallest instruction in a program that does something.
e.g.
A = 15 #value of variable A is 15
B=A–3 #value assigned to B
print ("Hello") #prints a message
Expression
Any legal combination of symbols that represent a value.
e.g.
15 #can be value only
3.9
A+5
(b+3)/7
8
03-08-2022
Comments
Comments are pieces of codes that the compiler discards or ignores and doesn’t
execute. It is to increase the readability/understandability of the program.
Comments can be –
1. Single line comments - #aaaaaa
2. Multiline comments -
Function
A function is a code/block that has a name, does a specific
task and can be reused in the program by its name.
9
03-08-2022
Variables
Named labels, whose values can be used and processed
during program run, are called variables.
lvalue rvalue
left hand side value right hand side value
x=10 #integer class
A variable pointing to a value of
print (x) a certain type, can be made to
x=10.7 #float class point to a value/ object of
print (x) different type. This is called
Dynamic Typing in Python.
x = ‘Computer’ #string class
print (x)
Variables
Multiple assignments:
a=b=c=10
x, y, z = 10, 20, 30 #assigns x=10, y=20, z=30
Swapping
x, y = 25, 40
x, y = y, x
Execution
a, b, c = 5, 10, 9
b, c, a = a+1, b+2, c-1 #will assign a=8, b=6, c=12
10
03-08-2022
Input/Output in Python
Input/reading Can be done using input function which reads a string,
printing can be done using print function
11
03-08-2022
Input/Output in Python
Variations of print
• converts item to string before printing
• two parameter to work with is sep(default is single space) and end (default is newline
>>>print ('Python')
>>>print ('Sum of 2 and 3 is' , 2+3)
>>>a=25
>>>print(''double of'', a, ''is'', a*2)
>>>print("Computer", "Science")
Computer Science #auto insertion of space in between,
#because of default sep value
>>>a, b, c = 10, 20 ,30
>>>print ("Numbers are: ", a, b, c)
Input/Output in Python
Variations of print:
print("Computer")
print("Science")
Output: Computer
Science
(print terminates with newline)
print("Computer", end='$')
print("Science")
Output: Computer$Science
12
03-08-2022
Input/Output in Python
Print variation with sep:
Assignment
1.+, -, *, / of two numbers
2.Area and perimeter of square
3.Area and perimeter of rectangle
4.Area and circumference of circle
5.Average of 5 numbers
6.Area of triangle on base and height
7.C to F and F to C
8.CM to feet and inches
13