Introduction To Python
Introduction To Python
Python Programming Language uses an interpreter to convert its instructions into machine language, so
that it can be understood by the computer.
NOTE: Interpreter converts high level language to low level language line by line.
Features of Python
1. Python is a high level language. It is a free and open source language.
2. Python is case-sensitive language.
3. Python is portable and platform independent.
4. Python has a rich library of predefined functions.
5. Python uses indentation for blocks and nested blocks.
Execution Modes of Python
There are two ways to use the Python interpreter:
1. Interactive mode: In the interactive mode, we can simply type a Python statement on the
>>> prompt directly. As soon as we press enter, the interpreter executes the statement and
displays the result(s). This mode is convenient for testing a single line code for instant
execution. But in the interactive mode, we cannot save the statements for future use and we
have to retype the statements to run them again.
2. Script mode: In the script mode, we can write a Python program in a file, save it and then
use the interpreter to execute it. Python files has an extension “.py”. To execute the Python
Program in script mode click Run–> Run Module from menu or press F5 from the
keyboard.
Python Terminology
Keywords: Keywords are reserved words. Each keyword has a specific meaning to the Python interpreter,
and we can use a keyword in our program only for the purpose for which it has been defined.
Identifiers: Identifiers are names used to identify a variable, function, or other entities in a program. The
naming conventions of identifiers in Python are as follows
1. An identifier cannot start with a digit.
2. Keywords can not be used as identifiers.
3. We cannot use special symbols like !, @, #, $, %, etc., in identifiers. (underscore can be used)
Examples of valid identifiers
1. num1
2. percentage
3. mark1
4. Stu_marks
Examples of invalid identifiers
1. 1_num (reason: Starting from digit)
2. marks@123 (reason : Using special character)
3. pass (reason: Keyword )
Variables: Variable in Python refers to an object — an item or element that is stored in the memory. Value
of a variable can be a string (e.g., ‘b’, ‘Global Citizen’), numeric (e.g., 345) or any combination of
alphanumeric
characters (CD67). for example
marks = 56
name = “Ananya”
Variable declaration is implicit in Python, means variables are automatically declared and defined when
they are assigned a value the first time. Variables must always be assigned values before they are used in
expressions as otherwise it will lead to an error in the program.
Example 1: Write a program to store the names of three students and display them.
name1 = "Aman"
name2 = "Sumit"
name3 = "Ananya"
print(name1)
print(name2)
print(name3)
OUTPUT:
Aman
Sumit
Ananya
Example 2: Write a program to display the sum of two numbers.
num1 = 20
num2 = 30
s = num1 + num2
print(s)
OUTPUT:
50
Comments: Comments are used to add a remark or a note in the source code. Comments are not executed
by interpreter. They are added with the purpose of making the source code easier for humans to
understand. In Python, a comment starts with # (hash sign).
Data Types :
Data type identifies the type of data values a variable can hold and the operations that can be performed
on that data. Various data types in Python are as follows:
1. Numbers
● Integer
● Boolean
● Floating Point
● Complex
2. Sequences
● Strings
● Lists
● Tuples
3. Mappings
● Dictionary
Number
This data type stores numerical values only. It is further classified into three different types: int, float and
complex.
Data type Description Examples
float This data type stores floating point numbers. -23.45, 40.26
complex This data type stores complex numbers. 1+2j, 21+5j
Boolean data type (bool) is a sub type of integer. This data type stores two values, True and False. Boolean
True value is non-zero. Boolean False is the value zero.
marks1 = 34
marks2 = 23.12
m1 = True
print(type(marks1))
print(type(marks2))
print(type(m1))
OUTPUT:
<class 'int'>
<class 'float'>
<class 'bool'>
Sequence :
It is an ordered collection of items, where each item is indexed by an integer. The three types of sequence
data types available in Python are Strings, Lists and Tuples.
1. String : String is a group of characters(like alphabets, digits or special characters including spaces).
Strings are enclosed in Single quotes(‘ ‘) or in double quotes(” “). for example
st1 = “Anuj”
st2 = ‘2342’
NOTE: Numerical functions can not be performed on String
Operators
Operators are special symbols which are used to perform some mathematical or logical operations on
values. The values on which the operators work are called operands. for example in 34 + 31, here ‘+’ is a
mathematical addition operator and the values 34 and 31 are operands. Python supports various kinds of
operators like
1. Arithmetic Operators :
Arithmetic operators are used to perform the following Mathematical operations.
Operator Operator
Explanation Examples
Symbol Name
>>>5 + 9
14
This operator help to add the two numeric
>>>12+8
values.
20
‘+’ Addition
>>>’a’ + ‘b’
This operator can also be used to
ab
concatenate two strings on either
>>>’C’ +
side of the operator
‘S’
CS
>>>7 * 4
This operator help us to find the product 28
‘*’ Multiplication
of two numeric values. >>>2 * 5
60
>>>7/2
This operator help us to divide the two
3.5
‘/’ Division numeric values and return the quotient
>>>8/2
with decimal.
4.0
>>>2**2
It performs exponential (power) 4
‘**’ Exponent
calculation on operands >>>4**3
64
2. Relational Operators :
Relational Operators compares the values and return the boolean value.
Operator
Operator Name Explanation Examples
Symbol
>>>9 > 4
This operator returns True if the number True
> Greater than on the left side of operator is larger >>>20 >
than number on the right side. 100
False
>>>56 < 43
This operator returns Tue if the number
False
< Less than on the left side of operator is smaller
>>>34 < 50
than number on the right side.
True
>>>4==4
This operator returns True , if both the True
== Equal to
operands are equal. >>>5==6
False
>>>7!=9
This operator returns True , if both the True
!= Not equal to
operands are not equal. >>>2!=2
False
3. Assignment Operators :
This operator assigns or changes the value of the variable on its left.
Operator Explanation Examples
>>>x = 7
>>>x
7
Assigns value of right-side operand
=
to left-side operand.
>>>a = “India”
>>>a
‘India’
4. logical Operators :
There are three logical operators in Python. The logical operator evaluates to either True or False
Operator Explanation Examples
>>>x = 7
>>>y = 9
>>>x >5 and y >7
and It returns True if all the conditions are True
True
>>>x<10 and y >10
False
>>>x = 7
>>>y = 4
>>>x >5 or y >7
or It returns True if any one of the condition is True
True
>>>x >15 or y >7
False
Expressions :
An expression is defined as a combination of constants, variables, and operators. An expression always
evaluates to a value.
1. 3+4 * (34 – 23)
2. 34 %3 + 3
Evaluation of expressions :
Evaluation of the expression is based on precedence of operators. (It determines which operator will be
evaluated first). Higher precedence operator is evaluated before the lower precedence operator.
The following table lists precedence of all operators from highest to lowest.
Order of
Operators Description
Precedence
10 or Logical operators
NOTE: The expression within bracket () is evaluated first. The expression is evaluated from left to right
for operators with equal precedence.
Q1. Evaluate the following expressions
1. 12 + 32 – 20
2. 25 * 2 – 8
3. 50 % 2 +3
4. (25 – 20) * 2
Ans.
1. 24
2. 42
3. 3
4. 10
input( ) function :
This function helps to take or accept data from the user. The syntax for input( ) is:
input ([“Any message which you want to display”])
for example:
>>>n1 = input("Enter any number : ")
Enter any number : 8
print( ) function :
This function help to display message or text on screen. The syntax for print() is:
print(value )
SUMMARY
1. Python is an open-source, high level, interpreter based language that can be used for a multitude of
scientific and non-scientific computing purposes.
4. The process of identifying and removing errors from a computer program is called debugging.
5. Trying to use a variable that has not been assigned a value gives an error.
6. There are several data types in Python — integer, boolean, float, complex, string, list, tuple, sets, None
and dictionary.
7. Operators are constructs that manipulate the value of operands. Operators may be unary or binary.
10. Python has print() function to output data to a standard output device.