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

AI Commands

Commands for AI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

AI Commands

Commands for AI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

>>>os.

system(‘cls’) //to clear screen

>>> Name = “Mahmood” //define object

>>>type(Name) // to know object type

>>>int(8.9) // will convert float into int and returns value of 8

>>>print(Name) // will return value stored in Name

>>>print(5+5)

>>> \n // to insert line between string

>>> pow() // raise to the power mathematical function

>>> from math import * //to import math functions

>>> listname.extend(2nd list name) //to concatenate 2 lists

>>> listname.append() //to add another item at the end of the list

>>> listname.insert(index,item) //to add item at indexed position and move other items to right

>>> listname.remove(item) //to remove item from list

>>> listname.clear() //to clear complete list

>>> listname.pop() //to delete last item

>>> listname.sort() //in ascending order

>>> listname.index(item) //to find specific item in the list

>>> listname.count(item) //to count specific item in the list

>>> listname.copy(listname) //to make a copy of the list

>>> ‘return’ is used to return value from function and break the code as well no statement following
return will be executed

>>> .get() //is used to extract stored data and also can provide default value in case searched word not
found

>>> for index in range(10) // all numbers from 0 to 9

>>> for index in range(3, 10) // all numbers from 3 to 9

Code:
friends = ["Ali", "Qasim", "Muhammad"]

for index in range(len(friends)):


print(friends[index])
>>> 2**3 // 2 raise to the power 3. Exponent of power

2D Lists:
number_grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]
print(number_grid[0][0)

Nested For Loops:


number_grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[0]
]

for row in number_grid:


for col in row:
print(col)

Translator:

Converts all wovels into g


def translate(phrase):
translation = ""
for letter in phrase:
if letter in "AEIOUaeiou":
translation = translation + "g"
else:
translation = translation + letter

return translation

print(translate(input("Enter a Phrase: ")))


Dealing with the Files

Lists:
Line = line.rstrip() to eliminate white spaces

If line.startswith(‘From’)

In lists we use indexing but in dictionaries we use ‘KEY’ and a ‘VALUE’, Dictionaries don’t follow order
they are a bag of values with labels.

Dictionaries:
.get to be used for Dictionaries to avoid traceback.
dictionary.get(key,O) // 0 is default value

Dict.key() to get Keys

Dict.values() to get values

Dict.items() to get keys and values

Tuples:
Tuples are immutable..

Very interested thing about tuples is that can be assigned multiple variables at one time

(x,y) = (8,9)

X=8

Y=9

For comparison tuples only compares till first true and the rest will not be compared.

(a,c) < (b,a) // True because 1st element is true

Dictionaries are actually list of tuples and can be put in order by

Di = sorted(di)
NUMPY
Arrays

Np.zeros((1,1)) // to create 2 d array of size 1,1

Np.ones((10,10))

Np.empty()//will return random numbers

Np.array(l) // to change list into array

x.shape // to check shape of x array

x.reshape // to change matrix dimensions

.dtype // to check data type

.ndim // to check dimension

All arithmetic operations can be performed on arrays // ,-,*,/,<,>

a>0 // Boolean output


a[a>0] // numeric output

x[1:10:2,::] for slicing pick odd numbers)

Exercie:

Chess board pattern using slicing/ football ground pattern

You might also like