Basic of Python (ARTIFICIAL INTELLIGENCE)
Basic of Python (ARTIFICIAL INTELLIGENCE)
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
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.
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
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}
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 }
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)
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)
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.
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
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