You are on page 1of 24

PYTHON

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.

Guido van Rossum is a Dutch programmer best known as the creator of


the Python programming language.
FAMILIARIZATION WITH THE BASICS OF PYTHON PROGRAMMING

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")

Python 3.7.7 (bundled)


>>> %Run name.py
Good Afternoon
>>>
PYTHON KEYWORDS
• Keywords are the reserved words in Python.
• We cannot use a keyword as a variable name, function name or any other identifier. They are
used to define the syntax and structure of the Python language.
• In Python, keywords are case sensitive.
• There are 33 keywords in Python 3.7. This number can vary slightly over the course of time.
PYTHON COMMENTS

• 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.

#This is a comment #print out Hello print('Hello')


PYTHON VARIABLES
• A variable is a named location used to store data in the memory. It is helpful to think of
variables as a container that holds data that can be changed later in the program.
• For example : number = 10
Here, we have created a variable named number. We have assigned the value 10 to the
variable.
You can think of variables as a bag to store books in it and that book can be replaced at
any time.

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)

# Output: x == y is False
print('x == y is',x==y)

# Output: x != y is True
print('x != y is',x!=y)

# Output: x >= y is False


print('x >= y is',x>=y)

# Output: x <= y is True


print('x <= y is',x<=y)
PYTHON LOGICAL OPERATORS:
Operat
Description Example
or
and Called Logical AND operator. If both the (a and b) is true.
operands are true then then condition
becomes true.
or Called Logical OR Operator. If any of the (a or b) is true.
two operands are non zero then then
condition becomes true.
not Called Logical NOT Operator. Use to not(a and b) is false.
reverses the logical state of its operand. If a
condition is true then Logical NOT operator
will make false.
PYTHON LOGICAL OPERATORS:

x = True
y = False

print('x and y is',x and y)

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

in Evaluates to true if it finds a variable in the x in y, here in results in a


specified sequence and false otherwise. 1 if x is a member of
sequence y.

not in Evaluates to true if it does not finds a x not in y, here not in


variable in the specified sequence and false results in a 1 if x is a
otherwise. member of sequence y.
PYTHON BITWISE OPERATORS:
Operat
Description Example
or
& Binary AND Operator copies a bit to the (a & b) will give 12
result if it exists in both operands. which is 0000 1100
| Binary OR Operator copies a bit if it exists (a | b) will give 61
in either operand. which is 0011 1101
^ Binary XOR Operator copies the bit if it is (a ^ b) will give 49
set in one operand but not both. which is 0011 0001
~ Binary Ones Complement Operator is unary (~a ) will give -60 which
and has the effect of 'flipping' bits. is 1100 0011
<< Binary Left Shift Operator. The left a << 2 will give 240
operands value is moved left by the which is 1111 0000
number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left a >> 2 will give 15
operands value is moved right by the which is 0000 1111
number of bits specified by the right
operand.
PYTHON OPERATORS PRECEDENCE
Operator Description
** Exponentiation (raise to the power)
~+- Ccomplement, unary plus and minus (method names for
the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += Assignment operators
*= **=
is is not Identity operators
in not in Membership operators
not or and Logical operators
DATA TYPES
Every value belongs to a specific data type in Python. Data type identifies the type of data values a variable can
hold and the operations that can be performed on that data.
EXPRESSIONS
An expression is defined as a combination of constants, variables, and operators.
An expression always evaluates to a value. A value or a standalone variable is
also considered as an expression but a standalone operator is not an expression.
Some examples of valid expressions are given below.

(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: = 20 + (30 * 40)

#Step 1 #precedence of * is more than that of + = 20 + 1200


#Step 2 = 1220
#Step 3

II. 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

You might also like