6238567-Chapter 3_Introduction to Python
6238567-Chapter 3_Introduction to Python
A programming language is a formal language that specifies a set of instructions that can be used to
produce various kinds of output. In simple Words, a programming language is a vocabulary and set of
grammatical rules for instructing a computer to perform specific tasks
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.
Why Python for AI?
Artificial intelligence is the trending technology of the future. You can see so many applications
around you. If you as an individual can also develop an AI application, you will require to know a
programming language. There are various programming languages like Lisp, Prolog, C++, Java and Python,
which can be used for developing applications of AI. Out of these, Python gains a maximum popularity
because of the following reasons:
Applications of Python
Python is used for a large number of applications.
Some of them are mentioned here:
Interactive Mode
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.
Python is a case-sensitive language. This means, Variable and variable are not
the same. Always name identifiers that make sense.
While, c = 10 is valid. Writing 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.
• • Create a info.py
NAME = "Ajay"
AGE = 24
• Create a main.py
import info
print(info.NAME)
print(info.AGE)
Ajay
24
In the above program, we create a constant.py module file. Then, we assign the constant value to
PI and GRAVITY. After that, we create a main.py file and import the constant module. Finally, we
print the constant value.
Note: In reality, we don't use constants in Python. The global or constants module is used throughout
the Python programs.
1. Python Numbers
Number data type stores Numerical Values. These are of three different types:
Tuples:
Tuples are a sequence of values of any type, and are indexed by integers. They are immutable.
Tuples are enclosed in ().
Example: t = (5,'program',2.5)
Python Operators
Operators are special symbols which represent computation. They are applied on
operand(s), which can be values or variables. Same operators can behave differently on different
data types. Operators when applied on operands form an expression. Operators are categorized as
Arithmetic, Relational, Logical and Assignment. Value and variables when used with operator are
known as operands.
Arithmetic Operators
Logical operators
Logical operators are the and, or, not operators.
Type Conversion
The process of converting the value of 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
This type of conversion is also called typecasting because the user casts (changes) the data
type of the objects.