0% found this document useful (0 votes)
7 views17 pages

Unit-5 (Introduction To Python)

The document provides an introduction to Python, detailing its features, applications, and modes of operation such as interactive and script modes. It covers fundamental concepts including keywords, identifiers, variables, data types, operators, and type conversion, along with examples. Additionally, it explains the use of the print() function for output in Python.

Uploaded by

Vibhanshu Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views17 pages

Unit-5 (Introduction To Python)

The document provides an introduction to Python, detailing its features, applications, and modes of operation such as interactive and script modes. It covers fundamental concepts including keywords, identifiers, variables, data types, operators, and type conversion, along with examples. Additionally, it explains the use of the print() function for output in Python.

Uploaded by

Vibhanshu Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class IX

ARTIFICIAL
INTELLIGENCE

Unit-5
(Introduction to Python)
PYTHON:
Python is an open source object oriented, high level programming language. It was developed by
Guido Van Rossum in February 1991 at the National Research Institute for Mathematics and Computer
Science in the Netherlands. It is based on ABC language, a teaching language created as a replacement of
BASIC language.

Features of Python:
1. Python language is freely available, which means it doesn't cost anything to use Python. It is open
source, so it can be freely modified and redistributed.
2. Python uses a few keywords and clear, simple English like structure. Thus it is very easy to read
and understand and is developer friendly.
3. Python can run on a variety of platforms - Windows, Linux, Macintosh, smart phones (Android,
iOS), etc. which means it is a portable language.
4. Python supports procedure-oriented as well as object-oriented programming.
5. It supports graphical user interface.
6. It is compatible with C, C+ languages, etc.
7. There are a variety of applications of this language. Python is used in Game development,
database applications, web applications, Artificial Intelligence, etc.
8. Python has a huge bunch of libraries with plenty of built-in functions to solve a variety of
problems.
9. Python has support for interactive mode which allows interactive testing and debugging of
snippets of code.

Applications of Python:
1. Web and Internet Development: Python offers many choices for web development
2. Scientific and Numeric: Python is widely used in scientific and numeric computing.
3. Education: Python is a superb language for teaching programming, both at the introductory level
and in more advanced courses.
4. Software Development: Python is often used as a support language for software developers, for
build control and management, testing, and in many other ways.
5. Business Applications: Python is also used to build ERP and e-commerce systems

Working in Python:

(i) in Interactive mode ii) in Script mode

Working in Interactive Mode:


Interactive mode of working means you type the command - one command at a time, and the Python
executes the given command there and then and gives you output.
In this kind of mode, you have to type the command in front of Python command prompt >>> . For
example, if you type 4+5 in front of Python Prompt, it will give you the result as 9.
To work in interactive mode, follow the process given below:

Step 1: Click Start button All Programs Python 3.8 IDLE (Python 3.8 32-bit )
Or
Click Start button All Programs Python 3.8 Python 3.8 (32-bit)
Step 2: It will open Python Shell where you'll see the Python prom

Step 3: Type commands in front of this Python prompt and Python will immediately give
you the result.

Working in Script Mode:


The script mode allows us to write all the commands in the form of a program file, and we can see all
output lines together. The commands in the program are executed sequentially. All the errors of the program
are shown together after the entire program execution is over.
To work in a script mode, you need to do the following:

Step 1: Click Start button All Programs Python 3.8 IDLE (Python 3.8 32-bit )
Step 2: Click File New File, in IDLE Python Shell.
Step 3: In the New window that opens, type the commands you want to save in the form of a program (or
script).
For example, for the simple Hello World program, you'll need to type following line :

Print ("Hello World!")

Step 4: Click File Save, and then save the file with an extension .py. For example [Link], Now your
program would be saved on the disk and the saved file will have .py extension.

How to Execute/Run a Script/Program:


After the program/script file is created, we can run it by the following given steps:
Step 1: Open the script/program created by you by using the command, File Open option.
Step 2: Click Run Run Module command in the open program/ script file's window. You may also press
F5 key.

Step 3: And it will execute all the commands stored in module / program/ script that you had opened and
show you the complete output in a separate Python Shell window.
Python IDE (Integrated Development Environment):
IDE typically provides code editor, compiler/ interpreter and debugger in one GUI (Graphical User
Interface). It encapsulates the entire process of code creation, compilation and testing which increases the
productivity of developers.
IDLE is Python's Integrated Development and Learning Environment. As the name may imply, IDLE is
very useful for developing new code or learning Python. On Windows this comes with the Python interpreter,
but in other operating systems you may need to install it through your package manager.

Python Character Set:


Character set is a set of valid characters that a language can recognize. A character represents any
letter, digit or any other symbol. The Python Character Set includes:

Letters A-Z, a-z


Digits 0-9
Special Symbols Space + - * / ** \ () [] // = != == < >
Whitespaces Blank space, tabs (-), carriage return (), newline, form feed
Other Python can process all ASCII and Unicode characters as part of data or
Characters literals.

Keywords:
The Keywords are some predefined and reserved words in python that have special meanings.
Keywords are used to define the syntax of the coding.
The keywords cannot be used as an identifier, functions, and variable name.
All the keywords in python are written in lower case except True, False, and None.
For python 3.8.0, there are 35 keywords in total and may change slightly with versions.

Identifiers:
The identifier is a name used to identify a variable, function, class, module, etc. It helps to
differentiate one entity from another.

Rules for writing identifiers:


1. Every identifier name should start with alphabets or underscore (_).
2. No spaces are allowed in identifier declaration.
3. Except underscore ( _ ) no other special symbol are allowed in the middle of the identifier.
4. An identifier is written with a combination of letters, numbers and special characters _ (underscore)
5. Keywords cannot be used as identifier.
Variables:
A variable is a named location used to store data in the memory. It is helpful to think of variable as a
container that holds data that can be changed later in the program.

Rules for writing Variables:


1. Every variable name should start with alphabets or underscore (_).
2. No spaces are allowed in variable declaration.
3. Except underscore ( _ ) no other special symbol are allowed in the middle of the variable
declaration
4. A variable is written with a combination of letters, numbers and special characters _ (underscore)
5. Keywords cannot be used as variable.

Examples of valid variable names are: last _name, name, name1, _name
Examples of invalid variable names: last-name, 9marks, marks%

Data types:
Data type represents the type of data stored into a variable or memory. Python offers the following
standard data types:

1. Numeric/Number Data Type: Numeric data type is used to store Numerical values. Python
supports the following numerical types:

(i) Integer: The integer datatype represents an integer number. An integer number without any
decimal point or fraction part. In Python, It is possible to store very large integer number as there is no limit
for the size of an int datatype.

Example:
20, 10, -50, -1002
y = 10
pin_code = 564512
(ii) Float: The float data type represents floating point numbers. A floating point number is a number
that contains a decimal point.

(iv) Complex: A complex number is a number that is written in the form of a + bj or a + bJ. Where,

a = Real Part of the number


b = Imaginary part of the number
j or J = Square root value of -1
a and b may contain integer or float number.

Example:
5+7j, 0.8+2j
com = 5+7j

2. Dictionary: A dictionary represents a group of elements in the form of key value pairs. Each key is
separated from its value by a colon (:), the items are separated by commas, and the entire dictionary is
enclosed in curly braces.
Example:

3. Boolen Type: The bool datatype represents boolean value True or False. Python internally
represents True as 1 and False as 0.
Example:
True, False
True + True = 2
True False = 1

4. Set: A set is an unordered collection of elements. The order of elements may not appear in the
same order. It means the elements may not appear in the same order as they are entered into the set. A set
does not accept duplicate elements. Sets are unordered so we can not access its element using index. Sets
are represented using curly brackets { }.

Example:
India
India
5. Sequence Type: Following are the sequence type:-
String
List
Tuple

(i) Strings: String represents group of characters. Strings are enclosed in double quotes or single
quotes.

Ex:-
str1
str1

(ii) Lists: A list represents a group of elements. A list can store different types of elements which can
be modified. Lists are dynamic which means size is not fixed. Lists are represented using square bracket [ ].

Ex:- data = [10, 20, -

(iii) Tuple: A tuple contains a group of elements which can be different types. It is similar to List but
Tuples are read- nted using parentheses
( ).

Ex:- data = (10, 20, -

OPERATORS: Operators are special symbols that perform an operation. Various types of operators are as
follows:
Arithmetic Operators
Relational Operators / Comparison Operators
Logical Operators
Assignment Operators
1. Arithmetic operators: Arithmetic Operators are used to perform basic arithmetic operations like
addition, subtraction, division etc.
There are 7 arithmetic operators in Python :

Example1. What will be the output produced by the following code?

Code:
A=9.2
B=2.0
C=4
D=21
print (A/4)
print (A//4)
print (B**C)
print (D//B)
print (A%C)
Output:
2.3
2.0
16.0
10.0
1.2

Example 2. Print the area of a circle of radius 3.75 metres.

Solution:
Radius = 3.75
Area = 3.14159*Radius**2
print (Area, 'sq. metre')

Output:
44.178609375 sq. metre

2. Relational operators: Relational operators are used for comparing two values. It either returns True or
False according to the condition. These operators are also known as Comparison Operators.
There are 6 Relational operators in Python :
Example:

a=5 a =
b=2 5
value = a < b
print(value) b=2
value = a >= b
Output: False print(value)

a=5 Output: True


b=2
value = a > b a=5
print(value) b=2
value = a == b
Output: True print(value)

a=5 Output: False


b=2
value = a <= b a=5
print(value) b=2
value = a != b
Output: False print(value)

3. Logical operators: Logical operators are used to connect more relational operations to form a
complex expression called logical expression. A value obtained by evaluating a logical expression is always
logical, i.e. either True or False.
There are 3 Logical operators in Python:
Example:

Logical and Logical or Logical not

a=5 a=5 a=5


b=2 b=2 b=2
c=3 c=3 print(not(a<b))
print(a>b and a>c) print(a>b or a>c)
print(a>b and a<c) print(a>b or a<c) Output:
print(a<b and a>c) print(a<b or a>c) True
print(a<b and a<c) print(a<b or a<c)

Output: Output:
True True
False True
False True
False False

4. Assignment Operators: Assignment Operators are used to assigning values to variables.


There are 8 Assignment operators in Python.

Example: a = 10
b = 20
m = 15 Output
y=a+b
print(y)
m+=10 30
print(m)
25
m-=10 15
print(m) 150
m*=10 15.0
print(m) 5.0
25.0
m/=10
print(m) 2.0

m%=10
print(m)

m**=2
print(m)

m//=10
print(m)
Operator Precedence and Associativity:
The computer scans an expression which contains the operators from left to right and performs
only one operation at a time. The expression will be scanned many times to produce the result. The order in
which various operations are performed is known as hierarchy of operations or operator precedence. Some
of the operators of the same level of precedence are evaluated from left to right or right to left. This is
referred to associativity.

Example:

Type Conversion: The process of converting one data type (integer, string,float etc.) to another data type is
called type conversion. Python has two types of type conversion:
1. Implicit Type Conversion
2. Explicit Type Conversion

Implicit Type Conversion: In the Implicit type conversion, python automatically converts one data type into
another data type
a=5 x = 10
For example:b = 2 y = 5.5
value = a/b total = x + y
print(value) print(total)
print(type(value)) print(type(total))

Output: Output:
2.5 15.5
<class 'float'> <class 'float'>
Explicit Type Conversion: In the Cast/Explicit Type Conversion, Programmer converts one data type into
another data type.

For example:

a=5 q = 20
b=2 u = '10'
value = a/b r = q + int(u)
print(int(value)) print(r)

Output:
2 Output:
30

Output Statements:
print( ) Function - The print() function is used to print the specified message to the output
screen/device. The message can be a string, or any other object.

Syntax:-
print( )

various methods to use print() function are as follows:

print ( ) This function is used to display a blank line.


- When a string is passed to the function, the string is displayed as it is.

Ex:- Output
I Love My India

I Love My India

Like share subscribe

10

Welcome
to
Geeky Shows

print(object) - We can pass objects like list, tuples and dictionaries to display the elements of those
objects.
Ex:-
data = [10, 20, -50, 21.3,
print(data)
print(data[0])
print(data[1])
Output- [10, 20, -50, 21.3, 'Geekyshows']
10
20

It separates string with given sep character. Character can be any string.
.
Ex:- Output
print("Like", "Share","Subscribe", sep=' ') Like Share Subscribe

print("Like", "Share","Subscribe", sep='***') Like***Share***Subscribe

print(variable list) This is used to display the value of a variable or a list of variable.

Ex:- a = 10 x = 20 x = 20
print(a) y = 30 y = 30
print(x, y)

Output: 10 Output: 20 30 Output: 20, 30

This is used to display the string along with variable.

Ex:-

m = 40
age = 62

Output: Value: 40 Output: My Name is Rahul and My age is 62

Input Statements:
input() Function: This function is used to accept input from the keyboard. The input( ) function is
used in the following manner:

Syntax:
Variable_Nmae = input (<prompt to be displayed>)

Example: name = input ( )

Whatever user gives as input, input function converts it into a string. If user enters an integer value
still input() function convert it into a string.
So if you need an integer you have to use type conversion.

Example:
Some Example Program in Python

Program-1: Program to obtain and print name, age and mobile no. of a person.

Code:

Output:

Program-2: Program to obtain three numbers and print their sum and average.

Code:

Output:
Program-3: Program to obtain length and breadth of a rectangle and calculate its area.

Code:

Output:

Program-4: Program to obtain temperature in Celsius and convert it into Fahrenheit using formula.

Code:

Output:

Program-5: What will be the output produced by the following code?

Code:
Output:

ARTIFICIAL
INTELLIGENCE

Chapter-7
(LISTs in Python)

You might also like