Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

6238567-Chapter 3_Introduction to Python

The document provides an introduction to Python, explaining its significance in artificial intelligence and detailing its applications. It covers fundamental concepts such as Python statements, variables, data types, operators, and input/output functions. Additionally, it highlights the importance of using an Integrated Development Environment (IDE) like IDLE for Python programming.

Uploaded by

piyushsen452
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

6238567-Chapter 3_Introduction to Python

The document provides an introduction to Python, explaining its significance in artificial intelligence and detailing its applications. It covers fundamental concepts such as Python statements, variables, data types, operators, and input/output functions. Additionally, it highlights the importance of using an Integrated Development Environment (IDE) like IDLE for Python programming.

Uploaded by

piyushsen452
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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:

Prepared by AJ, Department of Computer Science ISWK 1


In Detail……………….
When we install Python, an IDE named IDLE is also installed. We can use it to run Python on our
computer. IDLE (GUI integrated) is the standard, most popular Python development environment. IDLE is an
acronym of Integrated Development Environment. It lets one edit, run, browse and debug Python Programs
from a single interface. This environment makes it easy to write programs. Python shell can be used in two
ways, viz., interactive mode and script mode. Where Interactive Mode, as the name suggests, allows us to
interact with OS; script mode lets us create and edit Python source file.

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 Script/Program: Python statements written in a particular


sequence to solve a problem is known as Python Script/Program.

Prepared by AJ, Department of Computer Science ISWK 2


Python Statement and Comments
In this section we will learn about Python statements, why indentation is important and how to use
comments in programming.
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.
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.

Python Keywords and Identifiers


Keywords are the reserved words in Python used by Python interpreter to recognize the
structure of the program.

Prepared by AJ, Department of Computer Science ISWK 3


An Identifier is a name given to entities like class, functions, variables etc. It help to
differentiate one entity from another.

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.

Multiple words can be separated using an underscore, for example


this_is_a_long_variable.
Variables and Datatypes
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
y = 42

Prepared by AJ, Department of Computer Science ISWK 4


These declarations make sure that the program reserves memory for two variables with the
names x and y. The variable names stand for the memory location. It's like the two
shoeboxes, which you can see in the picture. These shoeboxes are labelled with x and y and
the corresponding values are stored in the shoeboxes. Like the two shoeboxes, the memory
is empty as well at the beginning.

Prepared by AJ, Department of Computer Science ISWK 5


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.
Non technically, you can think of constant as a shoe box with a fixed size of shoe kept inside which
cannot be changed after that.
Assigning Value to a constant in Python
In Python, constants are usually declared and assigned on a module. Here, the module means a
new file containing variables, functions etc. which is imported to the main file. Inside the module,
constants are written in all capital letters and underscores separating the words.

Example : Declaring and assigning value to a constant

• • Create a info.py

NAME = "Ajay"
AGE = 24

• Create a main.py
import info
print(info.NAME)
print(info.AGE)

• • When you run the program the output will be,

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.

Prepared by AJ, Department of Computer Science ISWK 6


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.

1. Python Numbers

Number data type stores Numerical Values. These are of three different types:

a) Integer & Long


b) Float / floating point
c) complex

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.
Complex:
The Complex Data Type in Python is the data type that is used for representing complex numbers.
As mentioned above a complex number is represented in the form of “a + bi'. In Python, we use the
j or J suffix instead of i, for denoting the imaginary part of the complex number.
For example, 3 + 3j.

Prepared by AJ, Department of Computer Science ISWK 7


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. Three types of sequence data type available in Python
are:
a) Strings
b) Lists
c) Tuples
String
String is an ordered sequence of letters/characters. They are enclosed in single quotes (‘ ‘) or double
(“ “). The quotes are not part of string. They only tell the computer where the string constant begins
and ends. They can have any character or sign, including space in them.
Example: name = “AJAY”
Long_sent=”This is a string”
Month=’January’
Lists
List is also a sequence of values of any type. Values in the list are called elements / items. These
are indexed/ordered. List is enclosed in square brackets.
Example: dob = [19,"January",1990]

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

Prepared by AJ, Department of Computer Science ISWK 8


Comparison operators
Comparison operators are used to compare values. It either returns True or False according
to the condition.

Logical operators
Logical operators are the and, or, not operators.

Prepared by AJ, Department of Computer Science ISWK 9


Assignment 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

Implicit Type Conversion


In Implicit type conversion, Python automatically converts one data type to another data type. This
process doesn't need any user involvement.
Example:

When we run the above program, the output will be

Prepared by AJ, Department of Computer Science ISWK


10
In the above program,
• We calculate the simple interest by using the variable priniciple_amount and roi with time divide
by 100
• We will look at the data type of all the objects respectively
• In the output we can see the datatype of principle_amount is an integer, datatype of roi is a float.
• Also, we can see the simple_interest has float data type because Python always converts smaller
data type to larger data type to avoid the loss of data.

Explicit Type Conversion


In Explicit Type Conversion, users convert the data type of an object to required data type.
We use the predefined functions like int(), float(), str(), etc to perform explicit type conversion.

This type of conversion is also called typecasting because the user casts (changes) the data
type of the objects.

Prepared by AJ, Department of Computer Science ISWK


11
Python Input and Output
Python Output
Using print() function We use the print() function to output data to the standard output device
(screen). We can also output data to a file.

An example is given below.

Prepared by AJ, Department of Computer Science ISWK


12
User input
. In python, input() function is used for the same purpose.

Prepared by AJ, Department of Computer Science ISWK


13

You might also like