0% found this document useful (0 votes)
12 views

Basic of Python (ARTIFICIAL INTELLIGENCE)

Uploaded by

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

Basic of Python (ARTIFICIAL INTELLIGENCE)

Uploaded by

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

BASICS OF PYTHON FOR USE IN AI PROJECTS

Programming Language is a language to write programs. It is a set of instructions that produces various
outputs.

A high-level language (HLL) is a programming language that enables a programmer to write programs
that are independent of a particular type of computer. Such languages are considered high-level
because they are closer to human languages and further from machine languages.

WHAT IS PYTHON

Python Programming Language is a high-level programming language created by

Guido van in 1989.

Features of Python Programming

 Non-restrictive Syntax
 No Declaration
 Inbuilt OOP Support
 Strong Tools in Debugging
 Python uses an elegant syntax. Making the programs that you write easier to read and
understand.
 Python is an easy-to-use language that makes it simple to get your program working. It is ideal
for prototype development and other ad hoc programming tasks, without compromising on
maintainability.
 Python comes with a large standard library that supports many common programming tasks
such as connecting to web servers, reading and modifying files, etc.
 Python’s interactive mode makes it easy to test short snippets of code. There is also a bundled
integrated development environment called IDLE.
 It can also be embedded into an application to provide a programmable interface through APIs.
 Python runs anywhere, including Mac OS X, Windows, Linux and Unix, with unofficial builds also
available for Android and iOS.
 It does not cost anything to download or use, or to include it in your application. Python can also
be freely modified and redistributed because while the language is copyrighted, it is available
under an open-source licence.

Some unique programming language features of Python are:

 A variety of basic data types are available: numbers (floating point, complex and unlimited-
length long integers), strings (both ASCCII and Unicode), lists, and dictionaries.
 Python supports OOP with classes and multiple inheritances.
 Code can be grouped into modules and packages.
 The language supports raising and catching exceptions, resulting in cleaner error handling.
 Data types are strongly and dynamically typed. Mixing incompatible types (e.g., attempting to
add a string and a number) causes an exception to be raised, so errors are caught sooner.
 Python contains advanced programming features such as generators and list comprehensions.
 Python’s automatic memory management frees you from having to manually allocate and free
memory in your code.

PYTHON FUNDAMENTALS

To understand the basics of Python, we will cover:


a. Python Variables
b. Python Data Types
c. Python Operators
d. Python Conditional Statements
e. Python Loops
f. Python Functions

A). VARIABLES IN PYTHON


Variables reserve memory locations to store values. The (=) sign is used to assign values to variables.
Ex.
S = 15

B). DATA TYPES


In Python, all data values are represented by objects. Each object or value has a data type. Python
Supports various data types such as numeric, list, tuple, string, set, dictionary.

1. Numeric
It stores numeric values. They are immutable which means that their value cannot be changed.
Python supports three Numeric data types:
 Integer type: It can hold all the integer values, i.e., all the positive and negative whole
numbers, for example, 11.
 Float type: It holds real numbers. It is represented by decimal and sometimes scientific
notations with E or e, indicating the power of 10 (3.5e2 = 3.5 x 102 = 357), for example,
6.24.
 Complex type: These are of the form x + aj, where x and y are floats and j represents the
square root of -1 (which is an imaginary number), for example, 10 + 6j.
2. List
List is actually the most versatile data type available in Python. It can be written as a list of
comma-separated values between square brackets.
Ex.
States = [ ‘Punjab’, ‘Haryana’, ‘Rajasthan’, 28 ]
Print (States)

3. Tuples
Tuples cannot be changed unlike Lists. Tuples use parentheses whereas Lists use square
brackets.
Ex:
Agnitio = ( ‘Kartik’, ‘Sahil’, ‘Himanshu’ )

4. Strings
Strings are created by simply enclosing characters in quotes. Python treats single and double
quotes equally.
Ex:
S = ‘Welcome to Agnitio’
D = “Agnitio”

5. Set
A Set is a collection of items without any specific order. Every element in a set is unique. A set is
typically created by placing all the items inside curly braces {}, separated by commas.

6. Union: Union of X and Y is a set of all the elements from both the sets. Union is performed using
| operator. Consider the example given below:
X = {10, 11, 12, 13}
Y = {12, 13, 14, 15}
print (X|Y)
Output:
{10, 11, 12, 13, 14, 15}

7. Intersection: Intersection of X and Y is a set of elements that are common in both sets.
Intersection is performed using & operator. consider the example given below:
X = {10, 11, 12, 13}
Y = {12, 13, 14, 15}
print (X & Y)
Output:
{12, 13}

8. Difference: Difference of X and Y (X – Y) is a set of elements that are only in X but not in Y.
Similarly, Y – X is a set of elements in Y but not in X. Consider the example given below:
X = {10, 11, 12, 13, 14}
Y = {13, 14, 15, 16, 17}
print (X-Y)
Output:
{10, 11, 12}

9. Symmetric Difference: Symmetric difference of X and Y is a set of elements in both X and Y


except those that are common in both. Symmetric difference is performed using ^ operator.
consider the example given below:

X = {11, 12, 13, 14, 15}


Y = {14, 15, 16, 17, 18}
print (X^Y)
Output:
{16, 17, 18, 11, 12, 13}

10. Dictionary
Dictionaries contain these ‘Key Value’ pairs enclosed within curly braces and Keys and Values
are separated by ‘:’.
Ex.
Dict = { ‘Name’ : ‘Kartik’, ‘Age’ :20 }

Dictionary operations are as follows:

1. Accessing elements from dictionary


Dict = { ‘Name’ : ‘Kartik’, ‘Age’ :20 }
print (Dict [‘Name’])
Output:
Kartik

2. Changing elements in a dictionary


Dict = { ‘Name’ : ‘Kartik’, ‘Age’ : 20 }
Dict [‘Age’] = 30
Dict [‘Address’] = ‘Delhi’
print (Dict)
Output:
{ ‘Name’ : ‘Kartik’, ‘Age’ : 30, ‘Address’ : ‘Delhi’ }

OPERATIORS IN PYTHON
Operators are the tools which can change the values of the variables. Consider the expression 7+3=10;
here 7 and 3 are operands and + is an operator.

I. Arithmetic Operators
These operators are used to do simple mathematical operations like +, -, *, /(division), % (Modulus),
//(floor division), **(exponent)

II. Comparison Operators


These operators compare the values on either side of their function and then decide the possible
relation among them.
== (Comparison), != (Not equal), >, <, >=, <=

III. Assignment Operators


An Assignment Operator (=) is the operator used to assign a new value to a variable like +=, -=, /=, *=,
%=, **=, //=

IV. Logical Operators


There are three type of logical operator:

AND
True True True
False True False
True False False
False False False

OR
True True True
False True True
True False True
False False False

NOT
True False
False True

V. Membership Operators
These operators are used to test whether a variable is found in a sequence (Lists, Tuple, Sets, Strings,
Dictionaries) or not. IN (Return True, if value is found), NOT IN (Return True, if value is not foun)

VI. Identity Operators


Theses operators are used to check if two variables are located on the same part of the memory. Two
variables that are equal does not mean that they are identical. IS (Return True, if the operands are
identical), IS NOT (Return True, if the operands are not identical)

 CONDITIONAL STATEMENTS
Conditional statements are used to execute a statement or a group of statements when some condition
is true.
There are namely three conditional statements-If, Elif, Else.

 PYTHON LOOPS
A loop statement allows us to execute such a statement or a group of statements multiple times as
required.

There are two types of loops:


 Infinite: When the condition will never become false
 Finite: At one point, the condition will become false and the program control will move out of
the loop

In Python, there are three loops:


 While
 For
 Nested

WHILE LOOP
In while loop, first the condition is checked and if it is true, control will move inside the loop and execute
the statements until the condition becomes false.
FOR LOOP
for loop also allows a part of the code to be repeated a certain number of times.

NESTED LOOPS
A Nested loop basically means a loop within a loop.

 PYTHON FUNCTIONS
Functions are an easy way to divide your code into useful blocks, making the code in order, more
readable, and efficient.

1. Code reusability
2. Code Organization and Documentation

ARTIFICAL INTELLIGENCE WITH PYTHON

Artificial Intelligence has been around for a long time now. As the demand for AI is growing, Python is
also gaining more and more importance.
1. Less Code
2. Pre-build Libraries
3. Ease of Learning
4. Platform-Independent
5. Massive Community Support

DEMAND FOR PROJCTS

1. More Computing Power


2. Data Generation
3. More Effective Algorithms
4. Broad Investment

You might also like