Python Introduction
Python Introduction
PROGRAMMING
FAMILIARIZATION WITH THE BASICS OF PYTHON PROGRAMMING
An ordered set of instructions to be executed by a computer to carry out a specific task is called a program, and the
language used to specify this set of instructions to the computer is called a programming language.
Python uses an interpreter to convert its instructions into machine language, so that it can be understood by the
computer. An interpreter processes the program statements one by one, first translating and then executing. This
process is continued until an error is encountered or the whole program is executed successfully. In both the cases,
program execution will stop.
On the contrary, a compiler translates the entire source code, as a whole, into the object code. After scanning the
whole program, it generates error messages, if any.
Python is a high-level general purpose programming language that
is used in a wide variety of application domains.
FEATURES OF PYTHON
• Python is a high-level language. It is a free and open-source language.
• It is an interpreted language, as Python programs are executed by an interpreter.
• Python programs are easy to understand as they have a clearly defined syntax and relatively simple
structure.
• Python is case-sensitive. For example, NUMBER and number are not same in Python.
• Python is portable and platform independent, means it can run on various operating systems and
hardware platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many popular web services and applications are built using
Python.
• Python uses indentation for blocks and nested blocks.
FIRST PROGRAM
print("Good Afternoon")
• Comments are very important while writing a program. They describe what is going on inside a
program, so that a person looking at the source code does not have a hard time figuring it out.
a, b, c = 5, 3.2, "Hello"
print (a) print (b) print (c)
PYTHON - BASIC OPERATORS
Python language supports following type of operators.
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators
PYTHON ARITHMETIC OPERATORS:
Operator Description Example
+ Addition - Adds values on either side of the a + b will give 30
operator
- Subtraction - Subtracts right hand operand a - b will give -10
from left hand operand
* Multiplication - Multiplies values on either a * b will give 200
side of the operator
/ Division - Divides left hand operand by b / a will give 2
right hand operand
% Modulus - Divides left hand operand by b % a will give 0
right hand operand and returns remainder
** Exponent - Performs exponential (power) a**b will give 10 to
calculation on operators the power 20
// Floor Division - The division of operands 9//2 is equal to 4 and
where the result is the quotient in which 9.0//2.0 is equal to 4.0
the digits after the decimal point are
removed.
PYTHON ARITHMETIC OPERATORS:
x = 15
y=4
# Output: x + y = 19
print("x + y =",x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
Write a Python program to find the area of a rectangle given that its length is 10 units and breadth is 20 units.
length = 10
breadth = 20
area = length * breadth
print(area)
PYTHON COMPARISON OPERATORS:
Operato
Description Example
r
== Checks if the value of two operands are equal or not, (a == b) is not true.
if yes then condition becomes true.
!= Checks if the value of two operands are equal or not, (a != b) is true.
if values are not equal then condition becomes true.
<> Checks if the value of two operands are equal or not, (a <> b) is true. This is
if values are not equal then condition becomes true. similar to != operator.
> Checks if the value of left operand is greater than the (a > b) is not true.
value of right operand, if yes then condition becomes
true.
< Checks if the value of left operand is less than the (a < b) is true.
value of right operand, if yes then condition becomes
true.
>= Checks if the value of left operand is greater than or (a >= b) is not true.
equal to the value of right operand, if yes then
condition becomes true.
<= Checks if the value of left operand is less than or (a <= b) is true.
equal to the value of right operand, if yes then
condition becomes true.
PYTHON COMPARISON OPERATORS:
x = 10
y = 12
# Output: x == y is False
print('x == y is',x==y)
# Output: x != y is True
print('x != y is',x!=y)
x = True
y = False
print('x or y is',x or y)
print('not x is',not x)
PYTHON MEMBERSHIP OPERATORS:
In addition to the operators discussed previously, Python has membership
operators, which test for membership in a sequence, such as strings, lists, or
tuples.
Operator Description Example
(i) 100 (iv) 3.0 + 3.14 (ii) num (v) 23/3 -5 * 7(14 -2) (iii) num – 20.4 (vi)
"Global" + "Citizen"
PRECEDENCE OF OPERATORS
• Evaluation of the expression is based on precedence of operators. When an expression contains different
kinds of operators, precedence determines which operator should be applied first. Higher precedence
operator is evaluated before the lower precedence operator. Most of the operators studied till now are
binary operators. Binary operators are operators with two operands. The unary operators need only one
operand, and they have a higher precedence than the binary operators. The minus (-) as well as + (plus)
operators can act as both unary and binary operators, but not is a unary logical operator.
• 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.
1. How will Python evaluate the following expression?
20 + 30 * 40
Solution: The two operators (–) and (+) have equal precedence. Thus, the first operator, i.e., subtraction is
applied before the second operator, i.e., addition (left to right). = (20 – 30) + 40
#Step 1 = -10 + 40
#Step 2 = 30
#Step 3