Fundaments of Python
Fundaments of Python
Python
Fundamentals
Introduction
Python was released in 1991. Although this version is
supposed to be backward incompatibles, later on many of its
important features have been back ported to be compatible
with version 2.7
Python Character Set
A set of valid characters recognized by python. Python uses the traditional
ASCII character set. The latest version recognizes the Unicode character set.
The ASCII character set is a subset of the Unicode character set
Letters :– A-Z,a-z
Digits :– 0-9
Special symbols :– Special symbol available over keyboard
White spaces:– blank space,tab,carriage return,new line, form feed
Other characters:- Unicode
Token
as finally or
continue if return
del in while
elif is with
except
Identifiers
A Python identifier is a name used to identify a variable,
function, class, module or other object.
* An identifier starts with a letter A to Z or a to z or an
underscore (_) followed by zero or more letters, underscores
and digits (0 to 9).
* Python does not allow special characters
* Python is a case sensitive programming language.
* Identifier must not be a keyword of Python.
Thus, Rollnumber and rollnumber are two different identifiers
in Python.
Some valid identifiers : Mybook, file123, z2td, date_2, _no
Some invalid identifier : 2rno,break,my.book,data-cs
Identifiers-continue
Some additional naming conventions
1. Class names start with an uppercase letter. All other
identifiers start with a lowercase letter.
\\ Backslash (\)
Types of Operators
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Operators continue
1. Arithmetic Operators
Arithmetic Operators are used to perform arithmetic operations like
addition, multiplication, division etc.
Operators Description Example
= Assigns values from right side operands to left side operand a=b
//= Perform floor division on 2 numbers and assigns the result to left operand. a = a//b
**= calculate power on operators and assigns the result to left operand. a = a**b
Operators continue
4. Logical Operators
Logical Operators are used to perform logical operations on the
given two variables or values.
a=30
b=20
if(a==30 and b==20):
print('hello')
Output :-
hello
Operators continue
6. Membership Operators
The membership operators in Python are used to validate whether a
value is found within a sequence such as such as strings, lists, or
tuples.
Operators Description Example
not in return true if value does not exists in the sequence, else false. a not in list
E.g.
a = 22
list = [22,99,27,31]
In_Ans = a in list
NotIn_Ans = a not in list
print(In_Ans)
print(NotIn_Ans)
Output :-
True
False
Operators continue
7. Identity Operators
Identity operators in Python compare the memory locations of two
objects.
is returns true if two variables point the same object, else false a is b
is not returns true if two variables point the different object, else false a is not b
a = 37
b=34
if (a is b):
print('both a and b has same identity')
else:
print('a and b has different identity')
Output :-
both a and b has different identity
Punctuators
Used to implement the grammatical and structure of a Syntax.Following
are the python punctuators.
Python program
print ("Emp Name: ", empname)
print ("Emp Role: ", emprole) indentation
A = 20 expression
print("Calling in proper sequence")
print("Calling in opposite sequence") statements
print('hello India')
Output :-
hello India
print(‘Computer',‘Science')
print(‘Computer',‘Science',sep=' & ')
print(‘Computer',‘Science',sep=' & ',end='.')
Output :-
Computer Science
Computer & Science
Computer & Science.
Input and Output
var1=‘Computer Science'
var2=‘Informatics Practices'
print(var1,' and ',var2,' )
Output :-
Computer Science and Informatics Practices