IX Unit 4 Python
IX Unit 4 Python
Unit-4
Python
Python: Python is the general purpose interpreted, interactive, object oriented and high level
scripting programming language. An object-oriented language means that it more emphasize on
data/object rather than the procedure. It was created by Guido Van Rossum during 1985-1990 and first
released in 1991. Python is designed t by highly readable. It uses English keywords frequently where as
other language use punctuation, and also has less syntactical construction than other language.
Important term: “Scripting language is a computer language with a series of command within the file
that is capable of being executed without being compiled. They are mainly two type server side scripting
language and client side language.”
Environment to run python program: There are two way to run python program interactive
mode and script mode. Interactive Mode, as the name suggests, allows us to interact with OS.
1
Artificial Intelligence
By: Ajay Mishra Sir
DPSGWL 2022
The above picture is interactive mode of python. In this user/programmer will directly run the single
instruction on python shell and see the result. Interactive mode is convenient for beginners and for
testing small pieces of code, as we can test them immediately.
In script mode, we type Python program in a file and then use the interpreter to execute the content
from the file. This will help to execute group of instruction at one time and see the meaning full result.
Python Statement: Instructions written in the source code for execution are called statements.
There are different types of statements in the Python programming language like Assignment
statement, Conditional statement, looping statements etc. These help the user to get the required
output. For example, n = 50 is an assignment statement. In Python, end of a statement is marked by a
newline character. Python can be extended to one or more lines or multiline statements using
parentheses (), braces {}, square brackets [], semi-colon (;), continuation character slash (\). When we
need to do long calculations and cannot fit these statements into one line, we can make use of these
characters.
Example:
2
Artificial Intelligence
By: Ajay Mishra Sir
DPSGWL 2022
Python Comments: A comment is text that doesn't affect the outcome of a code, it is just a piece
of text to let someone know what you have done in a program or what is being done in a block of code.
In Python, we use the hash (#) symbol to start writing a comment for single line comment and (“””) or
(‘’’) for multiline comment.
Keywords in Python: Keywords are predefined reserved words in Python used by Python
interpreter to recognize the structure of the program. These keywords used for special purpose by the
system. User/programmer will not use for its own purpose. List of keywords in python.
3
Artificial Intelligence
By: Ajay Mishra Sir
DPSGWL 2022
Identifier: An identifier is a noun which means it is a name given to entities like class, functions,
variables etc. It helps to differentiate one entity from another. There are some rules to define identifier.
1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z), digits(0 to
9) or an underscore(_).
2. An identifier cannot start with a digit. Example, 1variable is invalid but variable1 is valid.
3. Keywords cannot be used as identifiers.
4. We cannot use special characters like!,@,#,$,% etc. in our identifiers.
5. Identifier can be any length but maximum 32 characters identifiers will be a good programming
ethics.
Important Note:
Python is case sensitive language. This means variable and Variable are not the same.
Always name identifiers that make sense. While c=10 is valid but count =10 would make more
sense and it would be easier to figure out what it does even when you look at your code after a
long gap.
Multiple words can be separated using an underscore (_), for example Stu_First_Name,
Emp_Gross_Salary etc.
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 which can be changed later throughout programming. For
example: x=42 and y=45
x y
42 45
The above boxes are the memory location whose name is x and y. This location holds the value of x i.e.
42 and value of y i.e. 45. Some more example of variable is:
Constants: A constant is a type of variable whose value cannot be changed. It is helpful to think of
constants as containers that hold information which cannot be changed later.
Example: Declaring and assigning value to a constant
NAME = "Ajay"
AGE = 24
In the above example “Ajay” and 24 are the constant value assign to the variable NAME and AGE. This
type of variable you can use to define the value that are constant throughout the program like value of
PI, Speed of light in vacuum, gravity etc.
4
Artificial Intelligence
By: Ajay Mishra Sir
DPSGWL 2022
Datatypes: Every value in Python has a datatype. Since everything is an object in Python
programming, data types are actually classes and variables are instance (object) of these classes.
There are various data types in Python. Some of the important types are mentioned below in the image
Python Operators: Operators are special symbols which represent computation. Operators are
the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
Types of Operator
5
Artificial Intelligence
By: Ajay Mishra Sir
DPSGWL 2022
6
Artificial Intelligence
By: Ajay Mishra Sir