5.Getting Started With Python
5.Getting Started With Python
Features of Python
1. Python is a high-level language.
2. It is an interpreted language.
3. Python programs are easy to understand as they have well defined syntax and relatively
simple structure.
4. Python is case-sensitive.
5. Python has a rich library of predefined functions.
6. Python is portable and platform independent.
PYTHON KEYWORDS
Keywords are reserved words which have a specific meaning to the Python interpreter.
Eg. True, False, break
IDENTIFIERS
They are the names used to identify a variable, function, or other elements in a program.
Rules for naming an identifier
• The name should begin with an uppercase or a lowercase alphabet or an underscore sign (_).
This
may be followed by any combination of charactersa–z, A–Z, 0–9 or underscore (_).
• It can be of any length.
• It should not be a keyword or reserved word
• Special symbols cannot be used.
VARIABLES
Variables are containers for storing data. A variable in a program is uniquely identified by a
name.
The data present in variable can also be called as Object.
Object — An item or element that is stored in the memory of a variable.
Eg. a=10
In the above example a is a variable and 10 is a data/object.
Comment
Comments are used to add a remark or a note in the source code.
The symbol # is used to indicate a comment.
Eg.
#To find the sum of two numbers this line is a comment and hence will not be executed.
num1 = 10
num2 = 20
result = num1 + num2
Multi-Line Comment:
Use of Hash (#) multiple times in a program to add remarks.
Eg.
#This is a comment
#written in
#more than just one line
print("Hello, World!")
ID of an object
Every value or data item whether numeric, string, or other type is an object.
Every object in Python is assigned a unique identity (ID).
ID is similar to address of a memory location.
Eg. num1 = 20
id(num1)
1433920576 #identity of num1
DATA TYPES
OPERATORS
An operator is used to perform specific mathematical or logical operation on values.
Operands: The values that the operators work on are called operands.
Eg a+b
In the above example, a and b are operands and + is a operator.
1) Arithmetic Operators:
a) Addition(+): Adds the two numeric values Eg. num1 + num2
b) Subtraction(-): Subtracts the operand on the right from the operand on the left.
Eg. num1 - num2
c) Multiplication (*): Multiplies the two values. Eg. num1 * num2
d) Division(/): Divides the operand on the left by the operand on the right and returns the
quotient.
Eg. num1 = 8 num2 = 4
num2 / num1
0.5
e) Modulus(%) : Divides the operand on the left by the operand on the right and
returns the remainder
Eg. num1 = 13
num2 = 5
num1 % num2
3
f) Floor Division (//) or Integer division: Divides the operand on the left by the operand on
the right and returns the quotient by removing the decimal part.
Eg. num1 = 13
num2 = 4
num1 // num2
3
g) Exponent(**) : Performs exponential (power) calculation on operands.
Eg. num1 = 3
num2 = 4
num1 ** num2
81
2. Relational Operators
Let’s assume num1 = 10, num2= 0, num3 =10 to demonstrate the examples for the following
operators.
a) Equals to(==) : If the values of two operands are equal, then the condition is True, otherwise
it is False
Eg. num1 == num2
False
b) Not equal to(!=) If values of two operands are not equal, then condition is True,otherwise
it is False.
Eg. num1 != num2
True
c) Greater than(>): If the value of the left-side operand is greater than the value of the rightside
operand, then condition is True, otherwise it is False.
Eg. num1 > num2
True
d) Less than (<): If the value of the left-side operand is less than the value of the rightside
operand, then condition is True, otherwise it is False.
Eg. num1 < num3
False
e) Greater thanor equal to(>=) : If the value of the left-side operand is greater than or equal
to the value of the right-side operand, then condition is True, otherwise it is False
Eg. num1 >= num2
True
3. Assignment Operators
a) = Assigns value from right-side operand to left side operand
Eg. num1 = 2
num2 = num1
num2
2
b) += It adds the value of right-side operand to theleft-side operand and assigns the result to
the left-side operand
Note: x += y is same as x = x + y
Eg. num1 = 10
num2 = 2
num1 += num2
num1
12
c) -= It subtracts the value of right-side operand from the left-side operand and assigns the
result to left-side operand.
Eg. num1 = 10
num2 = 2
num1 -= num2
num1
8
d) *= It multiplies the value of right-side operandwith the value of left-side operand and
assigns the result to left-side operand.
Eg. num1 = 2
num2 = 3
num1 *= 3
num1
6
e) %= It performs modulus operation using two operands and assigns the result to left-side
operand.
Eg. num1 = 7
num2 = 3
num1 //= num2
num1
2
4. Logical Operators
a) Logical AND: Only if both the operands are True, then condition becomes True
Eg.
True and True=True
True and False= False
b) Logical OR : If any of the two operands are True, then condition becomes True
Eg.
True or True=True
True or False= True
c) Logical NOT: Used to reverse the logical state of its operand.
Eg. not True= False
not False= True
5. Identity Operators
Identity operators are used to determine whether the value of a variable is of a certain type or
not.
a) is : Evaluates True if the variables on either side of the operator point towards the same
memory location.
Eg. num1 = 5
num2 = num1
id(num1)
1433920576
id(num2)
1433920576
num1 is num2
True
b) is not: Evaluates to False if the variables on either side of the operator point to the same
memory location
Eg. num1 is not num2
False
6. Membership Operators
Membership operators are used to check if a value is a member of the given sequence or not.
a) in : Returns True if the variable/value is found in the specified sequence and False otherwise.
Eg. a = [1,2,3]
2 in a
True
'1' in a
False
b) not in : Returns True if the variable/value is not found in the specified sequence and False
otherwise.
Eg. a = [1,2,3]
10 not in a
True
1 not in a
False
EXPRESSIONS
An expression is defined as a combination of constants, variables, and operators.
The following represents a valid expression
(i) 100
(ii) 3.0 + 3.14
(iii) num
(iv) 23/3 -5 * 7(14 -2)
(v) num – 20.4
(vi) "Global" + "Citizen"
Note:
a) Parenthesis can be used to override the precedence of
operators. The expression within () is evaluated first.
b) For operators with equal precedence, the expression is
evaluated from left to right.
Evaluation of Expressions
1. 20 + 30 * 40
=20 + (30 * 40)
= 20 + 1200
= 1220
2. 20 - 30 + 40
= (20 – 30) + 40
= -10 + 40
= 30
3. 15.0 / 4 + (8 + 3.0)
= 15.0 / 4 + (8.0 + 3.0)
= 15.0 / 4.0 + 11.0
= 3.75 + 11.0
= 14.75
DEBUGGING
The process of identifying and removing bugs or errors, from a program is called debugging.
Types of Errors:
i) Syntax errors
ii) Logical errors
iii) Runtime errors
i) Syntax errors: Error is the structure and grammar of the program.
ii) Logical errors: A logical error is a bug in the program that causes it to behave incorrectly.
iii) Runtime errors: Error occurring during the run time or execution time is called as Runtime
errors.