INTRODUCTION TO PYTHON
1)What is a program?
A computer program is a collection of instructions that perform a specific task when executed by
a computer. It is usually written by a computer program in a programming language.
2)What is Python ?
Python is a widely used general-purpose, high-level programming language. It was created
by Guido van Rossum in 1991.
3)Why Python for AI?
4)Name the applications of AI.
5)Differentiate between the following: (any two)
a) Script mode and Interactive mode
Interactive mode: Python IDLE Shell account has >>> as Python prompt, where simple
mathematical expressions and single line Python commands can be written and can be
executed simply by pressing enter.
Script mode: In script mode, we type Python program in a file and then use the interpreter
to execute the content from the file.
Working in interactive mode is convenient for beginners and for testing small pieces of
code, as we can test them immediately. But for coding more than few lines, we should
always save our code so that we may modify and reuse the code.
b) Integer datatype and Float datatype
Integer & Long Integer Range of an integer in Python can be from -2147483648 to
2147483647, and long integer has unlimited range subject to available memory. Integers are
the whole numbers consisting of + or – sign with decimal digits like 100000, -99, 0, 17.
While writing a large integer value, don’t use commas to separate digits. Also, integers
should not have leading zeros.
Floating Point: Numbers with fractions or decimal point are called floating point numbers.
A floating-point number will consist of sign (+,-) sequence of decimals digits and a dot
such as 0.0, -21.9, 0.98333328, 15.2963. These numbers can also be used to represent a
number in engineering/ scientific notation. -2.0 x 105 will be represented as -2.0e5 2.0X10-
5 will be 2.0E-5
c) Single line and multiline comments
6) Define Identifier. Also list rules for naming the identifiers with examples.
For example-
Var1 , _var1,_1_var,var_1
7) Define the following terms :-
a)Variables- A variable is a named location used to store data in the memory.
For example, x = 42
b)Constants - A constant is a type of variable whose value cannot be changed
c)Data Types- specify the type of data that can be stored inside a variable.For example-
num=24
Here, 24 is integer.So data type is Int.
8)Write a code to calculate Area and Perimeter of a rectangle.
# Area and Perimeter of a rectangle
L=int(input("Length"))
B=int(input("Breadth"))
Area=L*B Perimeter=2*(L+B)
print("Area:",Area)
print("Perimeter:",Perimeter)
9)Explain the different types of Data Types.
The following are some types of Data Types :-
1. Numbers – Numerical values are stored in the Number data type.
i) Int : Integers are the whole numbers consisting of + or – sign with decimal digits
like 100000, -99, 0, 17. value.
ii) Float : Numbers with fractions or decimal point are called floating point numbers
such as 0.0, -21.9, 0.98333328, 15.2963.
iii) None- This is special data type with single value. It is used to signify the absence
of value/false in a situation. It is represented by None.
2. Sequence- A sequence is an ordered collection of items, indexed by positive integers. It
is a combination of mutable and non-mutable data types.
i) Strings- String is an ordered sequence of letters/characters. They are enclosed in
single quotes (‘ ‘) or double (“ “).
Example: name=’Rakesh Kumar’
ii) Lists- Values in the list are called elements / items. These are indexed/ordered
same way as array. List is enclosed in square brackets.
Example :dob = [19,"January",1990]
iii) Tuples- Tuples are a sequence of values of any type, and are indexed by integers.
They are immutable and also have duplicate values. Tuples are enclosed in ().
3. Sets- is an unordered collection of values, of any type, with no duplicate entry.
Example :
a = {1,2,2,3,3,3}
a {1,2,3}
4. Maps- This data type is unordered. Dictionaries fall under Mappings.
Example
d = {1:'Ajay','key':2}
10) What are the Operators ? Name the categories.
Operators are special symbols which represent computation. They are applied on operand(s), which
can be values or variables.
Arithmetic Operators
Comparison operators
Logical Operators
Assignment operators