Вы находитесь на странице: 1из 5

len(<object name>)

type(a)
% remainder
**exponent
z= a+bj
z.real=a
z.imag=b

name[index]= gives the value at that position


eg. computers[2]=m

list:
z =[a,b,c,d,g,f]
to change value of list:
a[2]=x
z=[a,b,x,d,g,f]

tuple:
a=(1,2.3,4,5)

to check position of a value in the memory:


id(value)

floor division:
15//4=3 and not 3.75

modulus:
7.2%3=1.2 (remainder)

x+=y is the same as x=x+y


x*=y is x=x*y
and so on

equality:
a==b

suppose:
a=7.8
then,
int(7.8)=7
float(7.8)=7.8
complex(7.8)=7.8+0j
str(7.8)="7.8"

name=input("what is your name")

print(name)

if statements:
eg:
ch=1
count=0
if ch<=8:
ch+=1
count+=1

if else:
a=int(input("enter a number'))
if a>=0:
print("a is positive")
else:
print("a is negative")

if-elif:
if(conditional)
statement
elif(conditional)
statement
else(conditional)
statement

range(lower limit, upper limit, step value)

a in range()

a not in range()

for loop:
for a in sequence
statements to repeat

note upper limit of range is not counted


eg: range(1,5)=[1,2,3,4]

while loop:
eg:
while (logical expression)
statements

string slices
name[n:m:step value]

to create a list:
a=list[value]
len(a) gives the length of the list
a[n:m:step value]

List commands:
say L=[1,2,3,4,5]
1) L.apprend(item), adds an item at the end of a list
2) L[index]=new value, adds an item at the respective indexes
3)del L[index]=deletes the value at the respective index in the list
4)L.pop(index)=deletes the vlaue at the respecive index AND shows the value
deleted, if no index specified it will remove last value
5)L.index(value)=returns the index of the value from the list
6)say L2=[6,7], then L.extend(L2) is [1,2,3,4,5,6, 7]
7)L.insert[index,value]= Inserts a new value at its dedicated position
8)L.clear()=erases the list
9)L.count(value)=counts the number of occurences of that specific value
10)L.reverse()=reverses items of the list
11)L.sort()=sorts the items of list in ascending order
L.sort(reverse=true) for descending order
Dicitionaries:
Dictionary name={key:value,key:value,key:value..n.}...note key has to be of
immutable type (cannot be a list)
say d={1: "a" , 2: "b" , "a":"c"}
d[1]="a"
eg: for key in d:
print(key ":" d[key])
d.keys()=prints all the keys of the dictionary
d.value()=prints all the values
d[key]=value, a new record is added or changed

Dictionary commands:
1)del d[key]
2)d.pop[key], d.pop(key, if key not present then message to show)
3)key in d, k not in d, checking for presence
4)json.dumps(d,indent=n)
5)len(d)
6)clear(d)
7)d.get(key, message to show if key does not exist)
8)say d2={1: "f",2: "k", 3= "g"} then d.update(d2)= {"a":"c",1: "f",2: "k", 3=
"g"}, the data present in d has been overriden by d2, notice the order of keys have
changed...no order is specifies in dictionary

Transfering files:
1) To read data from a csv file into the dataframe:
DF=pandas.read_csv(File path, name=[column names], header=none, skiprows=<n>,
nrows=number of rows to display, sep=<any character that you want to appear bw each
feild>)
name=[column names] allows you to give the name of columns a sper your choice
header=none has no column name
nrows=x allows you choose how many rows to display
sep="," will show a comma bw each feild in a record....denmark ; peru ; beat

2)To save data from dataframes into csv files:


Df.to_csv(filepath,sep=sepchar, na_rep="NULL")
na_rep="string" will assign any value at an empty place

if there is no value in a dataframe then we assign a NaN value so that we can


transfer:
df.loc[row number, "column name"=np.NaN

3)To read data from a sql database:


steps:
i) Python comes with sqlite3
import sqlite as sq
ii) make connection to sql database
conn=sq.connect(database name and file path)
eq: databse name is new.db then conn=sq.connect("c:\\sqlite3\\new.db")
iii)df=pandas.read_sql("Sql statement..eg: "Select * from stud", conn)

4)To store a dataframes data in an sql database:


steps:
i)import pandas and sqlite3 libraries
ii)conn.sq.connect(file path and name)
iii)df.to_sql(<tablenam , conn, [if_exists="append"]

Commands of mysql
Create database <name>;
use <name>;
select <column names> from tablename;
select* from table name;
select distinct <column name> from tablename; #does not give repeated records
show tables;
desc or describe<tablename> ;

to perform calculations:
select a+b;
select a*b;

to display date=> select curdate();

to change name of column


select<columname> As [columnalias] from <tablename>
eg: select time As "event date" from events;

select <columname 1>, IFNULL (columname 2, "Message to display"); #this is used


when a value has to be displayed in place null

putting text to appear in every row: select salesman_name, "gets the comission",
comm*100,"%" <=== here gets the commission and % appears in every row

Where clause:
select<columname> from tablename where <condition>

note: when using string <> represent not equal to:


eg: where <>delhi
where(grade="a" or grade="b")
where (grade="c" and grade<75)
where(not grade="f")
where f between 30 and 50
where city in("Mumbai", "Delhi")
where city not in("Mumbai", "Delhi")
where pin like "13%" starting with 13
where pin like "%13" ending with 13
where depthno is null
order by <columname>

Databases:
Create database name;
show databases;
drop database <name>;

Tables:
Create table (table name)
(column name datatype(size) column constraints

);

Constraints:
Unique: makes sure that all the values in that column are unique, allows null value
primary key: it declares the column as primary key
default "value": it specifies a value automatically for that column when no value
is entered
check (conditional) : limits values that can be entered in the column
Foreign Key constraint=> columname datatype(size) References tablename (columname)
[ON UPDATE CASCADE][ON DELETE CASCADE]
OR you can create a constraint=> constraint<name-of-constraint><definition of
constraint>

DML commands:
-insert into <tablename> (column list)
-values(values as per subtending column list)
-eq: insert into branch1
select*from branch2
where gross>7000
-commit; to make changes permanent
-update:
update items
set rol=400
where rol=300;
-delete from <tablename>
[where=conditional]===> if where not included then it will empyt whole table

DDL commands:
-Alter table <tablename>
add <columname><datatype><size>constraint..etc.
-Alter table <tablename>
modify (columname newdatatype (newsize)[First/after column]==>to specify postion
of column
-Alter table <tablename>
change <old columname> <new columname> datatype....
-Alter table <tablename>
drop primary key, drop foreign key <constraint name>, drop <columname>

drop table [if exists] <tablename>