Basics of Programming
language
What is programming language
Language is set of instruction many language
passing instructions using language.
Should follow some of instructions
Why Programming Language?
To communicate computer.
Set of instructions i.e, programs which is
used to develop applications is a software.
It is language which is predefined language
to develop applications.
Using computer language to communicate
software with other devices.
Need of programming language
We communicate with machine for some task
to be performed.
To share information we communicate.
We the person wants to communicate with
machine
Factorial 5! = 120
But its difficult to identify factorial of 120!
= ?... Directly we can’t identify.
To perform complex operation
Why language
We want to communicate with computer
Persons in order to pass instruction in
machine code only
Computer understandable language, i.e in
binary language
Can we anyone pass in binary language, NO
it is very complex.
First, we have to learn programming
language
i.e C, C++, .net, java, python – High Level
Language
Compiler in Programming language
Compiler is predefined software
Ready for conversions of source code to binary
code
Compiler converts all instruction to binary
Now machine code is ready.
That, we can pass as an input to computer.
Now computer is able to produces output.
To perform complex operations.
Ex: ATM if it doesn’t exist, process to withdraw
would be very difficult to follow up manually.
Chapter 5
» Introduction to Python
» Python Keywords
» Identifiers
» Comments
» Data Types
» Operators
» Expressions
» Statement
» Input and Output
» Type Conversion
» Debugging
Data types in Python
Mutable and Immutable Data Types
Object and its identifier
OPERATORS
Arithmetic Operators
Addition
Adds the two numeric values on either side of the
operator
This operator can also be used to concatenate two
strings on either side of the operator
>>> num1 = 5
>>> num2 = 6
>>> num1 + num2
11
>>> str1 = "Hello"
>>> str2 = "India"
>>> str1 + str2
'HelloIndia'
Subtracts the operand on the right from the operand on the
left
>>> num1 = 5
>>> num2 = 6
>>> num1 - num2
-1
Multiplies the two values on both side of the operator
Repeats the item on left of the operator if first operand is a
string and second operand is an integer value
>>> num1 = 5
>>> num2 = 6
>>> num1 * num2
30
>>> str1 = 'India'
>>> str1 * 2
'IndiaIndia'
Division/Modulus
Divides the operand on the left by the operand on the
right and returns the quotient
>>> num1 = 8
>>> num2 = 4
>>> num2 / num1
0.5
Divides the operand on the left by the operand on the
right and returns the remainder
>>> num1 = 13
>>> num2 = 5
>>> num1 % num2
3
Floor Division (//)
Divides the operand on the left by the operand on
the right and returns the quotient by removing
the decimal part. It is sometimes also called
integer division.
>>> num1 = 13
>>> num2 = 4
>>> num1 // num2
3
>>> num2 // num1
0
Exponent (**)
Performs exponential (power) calculation on
operands. That is, raise the operand on the
left to the power of the operand on the right
>>> num1 = 3
>>> num2 = 4
>>> num1 ** num2
81
Relational Operators
Logical Operators
Logical AND >>> True and True
If both the True
>>> num1 = 10
operands are True,
>>> num2 = -20
then condition >>> bool(num1 and num2)
becomes True True
>>> True and False
False
>>> num3 = 0
>>> bool(num1 and num3)
False
>>> False and False
False
Logical OR
If any of the two >>> True or True
operands are True, True
then condition >>> True or False
becomes True
True
>>> bool(num1 or
num3)
True
>>> False or False
False
Logical NOT
Used to reverse the >>> num1 = 10
logical state of its >>> bool(num1)
operand True
>>> not num1
>>> bool(num1)
False
Assignment Operators
= (Equal Symbol)
Assigns value from right-side operand to left-
side operand
>>> num1 = 2
>>> num2 = num1
>>> num2
2
>>> country = 'India'
>>> country
'India'
+=
It adds the value of right- >>> num1 = 10
side operand to the left- >>> num2 = 2
>>> num1 +=
side operand and assigns num2
the result to the left-side >>> num1
operand 12
Note: x += y is same as x = >>> num2
x+y 2
>>> str1 = 'Hello'
>>> str2 = 'India'
>>> str1 += str2
>>> str1
'HelloIndia'
-=
It subtracts the value of right-side operand
from the left-side operand and assigns the
result to left-side operand
Note: x -= y is same as x = x – y
>>> num1 = 10
>>> num2 = 2
>>> num1 -= num2
>>> num1
8
*=
It multiplies the value of right-side operand with the value
of left-side operand and assigns the result to left-side
operand
Note: x *= y is same as x = x * y
>>> num1 = 2
>>> num2 = 3
>>> num1 *= 3
>>> num1
6
>>> a = 'India'
>>> a *= 3
>>> a
'IndiaIndiaIndia'
/=
It divides the value of left-side operand by the
value of right-side operand and assigns the
result to left-side operand
Note: x /= y is same as x = x / y
>>> num1 = 6
>>> num2 = 3
>>> num1 /= num2
>>> num1
2.0
%=
It performs modulus operation using two
operands and assigns the result to left-side
operand
Note: x %= y is same as x = x % y
//=
It performs floor division using two operands
and assigns the result to left-side operand
Note: x //= y is same as x = x // y
**=
It performs exponential (power) calculation
on operators and assigns value to the left-side
operand
Note: x **= y is same as x = x ** y
>>> num1 = 2
>>> num2 = 3
>>> num1 **= num2
>>> num1
8
Identity Operators
Identity operators are used to determine
whether the value of a variable is of a certain
type or not.
Identity operators can also be used to
determine whether two variables are
>>> num2 = num1
referring to the same object or not
>>> id(num1)
1433920576
>>> num1 = 5 >>> id(num2)
>>> type(num1) is int 1433920576
>>> num1 is num2
True True
>>> num1 is not
num2
False
Membership Operators
Membership operators are used to check if a value
is a member of the given sequence or not.
>>> a = [1,2,3]
>>> 2 in a
True
>>> '1' in a
False
>>> a = [1,2,3]
>>> 10 not in a
True
>>> 1 not in a
False
Data Types in Python
Numeric
Sequence
Sets
None
Dictionary
Operators:
Arithmetic Operators
Assignment Operators
Logical Operators
Relational Operators
Identity Operators
Membership Operators
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.
(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.
Binary operators are operators with two operands.
The unary operators need only one operand,
They have a higher precedence than the binary operators.
The minus (-) as well as + (plus) operators can act as both
unary and binary operators.
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.
How will Python evaluate the following expression?
20 + 30 * 40 20 - 30 + 40
= 20 + (30 * 40) = (20 – 30) + 40
= 20 + 1200 = -10 + 40
= 30
= 1220
STATEMENT
A statement is a unit of code that the Python
interpreter can execute.
>>> x = 4 #assignment statement
>>> cube = x ** 3 #assignment statement
>>> print (x, cube) #print statement
4 64
INPUT AND OUTPUT
In Python, we have the input() function for taking the user input.
The input() function prompts the user to enter data.
It accepts all user input as string.
The user may enter a number or a string but the input() function
treats them as strings only.
The syntax for input() is: input ([Prompt])
Prompt is the string we may like to display on the screen prior to
taking the input, and it is optional.
When a prompt is specified, first it is displayed on the screen after
which the user can enter data.
The input() takes exactly what is typed from the keyboard,
converts it into a string and assigns it to the variable on left-
hand side of the assignment operator (=).
Entering data for the input function is terminated by pressing the
enter key.
Output Statement : using print()
print(value [, ..., sep = ' ', end = '\n'])
sep: The optional parameter sep is a separator
between the output values. We can use a character,
integer or a string as a separator. The default
separator is space.
end: This is also optional and it allows us to specify
any string to be appended after the last value. The
default is a new line.
print("Hello")
print(10*2.5)
print("I" + "love" + "my" + "country")
print("I'm", 16, "years old")
Example for input()
>>> fname = input("Enter your first name: ")
Enter your first name: Arnab
>>> age = input("Enter your age: ")
Enter your age: 19
>>> type(age)
<class 'str'>
#function int() to convert string to integer
>>> age = int( input("Enter your age:"))
Enter your age: 19
>>> type(age)
<class 'int'>
TYPE CONVERSION
Explicit Conversion
Implicit Conversion
The program was expected to display double the value
of the number received and store in variable num1.
>>>num1 = input("Enter a number and I'll double it: ")
>>>num1 = num1 * 2
>>>print(num1) # Result will be 22
This is because the value returned by the input function is a
string ("2") by default.
>>>num1 = input("Enter a number and I'll double it: ")
>>> num1 = int(num1) #convert string input to integer
>>> num1 = num1 * 2
>>> print(num1)
Explicit Conversion
Explicit conversion, also called type casting
happens when data type conversion takes place
because the programmer forced it in the program.
The general form of an explicit data type conversion is:
(new_data_type) (expression)
there is a risk of loss of information since we are forcing
an expression to be of a specific type.
For example, converting a floating value of
x = 20.67 #into an integer type.
int(x) #will discard the fractional
part .67.
Explicit type conversion functions in Python
Function : Description
int(x) : Converts x to an integer
float(x) : Converts x to a floating-point
number
str(x) : Converts x to a string
representation
chr(x) : Converts ASCII value of x to
character
ord(x) : returns the character associated
with the ASCII code x
Implicit Conversion
Implicit conversion, happens when data type
conversion is done automatically by Python
and is not instructed by the programmer
num1 = 10 #num1 is an integer
num2 = 20.0 #num2 is a float
sum1 = num1 + num2 #sum1 is sum of a float
and an integer
print(sum1)
print(type(sum1))
DEBUGGING
A programmer can make mistakes while
writing a program, and hence, the program
may not execute or may generate wrong
output.
The process of identifying and removing such
mistakes, also known as bugs or errors, from a
program is called debugging.
Three Categories:
i) Syntax errors
ii) Logical errors
iii) Runtime errors
Syntax error :
If any syntax error is present, the interpreter shows
error message(s) and stops the execution there.
Logical error :
A logical error is a bug in the program that causes it to
behave incorrectly. A logical error produces an undesired
output but without abrupt termination of the execution of
the program.
Ex: To find the average of two numbers 10 and 12 and we
write the code as 10 + 12/2. it would run successfully
and produce the result 16.
The correct code to find the average should have been
(10 + 12)/2 to give the correct output as 11.
Runtime Error:
A runtime error causes abnormal termination of
program while it is executing.
Runtime error is when the statement is correct
syntactically, but the interpreter cannot execute it.
Runtime errors do not appear until after the
program starts running or executing.
Ex: We have a statement having division operation
in the program. By mistake, if the denominator
entered is zero then it will give a runtime error like
“division by zero”.