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

print(5+10)

#This is comment in Python


# Exponentiation
print(4 ** 2)
Ans: 16
## Concatenate String
In: 'ab' + 'cd'
Out: abcd
#Variable String
desc="compound interest"
// To convert into different datatypes
# Definition of savings and result
savings = 100
result = 100 * 1.10 ** 7

# Fix the printout

print("I started with $" + str(savings) + " and now have $" + str(result) + ". A
wesome!")

# Definition of pi_string
pi_string = "3.1415926"

# Convert pi_string into float: pi_float

pi_float=float(pi_string)
In [5]: "I said " + ("Hey " * 2) + "Hey!"

Out[5]: 'I said Hey Hey Hey!'


In [7]: "I can add integers, like " + str(5) + " to strings."

Out[7]: 'I can add integers, like 5 to strings.'


In [1]: True + False

Out[1]: 1

//PYTHON LIST
As opposed to int, bool etc, a list is a compound data type. Python list can als
o contain another list. you can group values together:
a = "is"
b = "nice"
my_list = ["my", "list", a, b]

# Create list areas


areas = ["hallway",hall, "kitchen",kit, "living room", liv,"bedroom" ,bed, "bath
room", bath]
# Print areas
print(areas)
Output: ['hallway', 11.25, 'kitchen', 18.0, 'living room', 20.0, 'bedroom', 10.7
5, 'bathroom', 9.5]
EXAMPLE
In [2]: [1 + 2, "a" * 5, 3]
Out[2]: [3, 'aaaaa', 3]
house = [["hallway", hall],
["kitchen", kit],
["living room", liv],
["bedroom",bed],
["bathroom",bath]]
[['hallway', 11.25], ['kitchen', 18.0], ['living room', 20.0], ['bedroom', 10.7
5], ['bathroom', 9.5]]
//TO GET ELEMENTS FROM LIST
x = list["a", "b", "c", "d"]
x[1]
x[-3] # same result!
//TO GET SUM
x = ["a", "b", "c", "d"]
print(x[1] + x[3]) // or print(x[-4]+x[-2])
//LIST SLICING
Selecting single values from a list is just one part of the story. It's also pos
sible to slice your list, which means selecting multiple elements from your list
. Use the following syntax:
my_list[start:end]
The start index will be included, while the end index is not.
The code sample below shows an example. A list with "b" and "c", corresponding t
o indexes 1 and 2, are selected from a list x:
x = ["a", "b", "c", "d"]
x[1:3]
-----------------
# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.7
5, "bathroom", 9.50]
# Use slicing to create downstairs
downstairs=areas[:6]
# Use slicing to create upstairs
upstairs=areas[6:]
OUTPUT
['hallway', 11.25, 'kitchen', 18.0, 'living room', 20.0]
['bedroom', 10.75, 'bathroom', 9.5]
-----------
In [1]: x = [["a", "b", "c"],
... ["d", "e", "f"],
... ["g", "h", "i"]]
... x[2][0]
... x[2][:2]
Out[1]: ['g', 'h']
----------
//TO CHANGE THE VALUES IN THE LIST
x = ["a", "b", "c", "d"]
x[1] = "r"
x[2:] = ["s", "t"]
OUTPUT
['a', 'r', 's', 't']
------------------------
ADDING ELEMENTS TO LIST
x = ["a", "b", "c", "d"]
y = x + ["e", "f"]
--------------------------
DELETING ELEMENTS
x = ["a", "b", "c", "d"]
del(x[1])
------------
MANIPULATING LISTS
x=["a","b","c"]
#To assign the list to another variable and make changes use the following comma
nd
y=list(x)
y=x[:]
---------
LENGTH
# Print out length of var1
print(len(var1))
//USE help(name_of_function) TO KNOW ABOUT THAT FUNCTION
-------------
SORT THE LIST
that sorted() takes three arguments: iterable, key and reverse.
key=None means that if you don't specify the key argument, it will be None. reve
rse=False means that if you don't specify the reverse argument, it will be False
//Use this if you don't want to use the key arguement
sorted(___, reverse = ___)
----------------
METHODS
Python objects also come
with a bunch of so-called "methods". You can think of methods as _functions_ tha
t "belong
to" Python objects.
room = "poolhouse"
room_up=room.upper()
print(room)
print(room_up)
OUTPUT :
poolhouse
POOLHOUSE

print(room.count("o"))
OUTPUT
3
--index(), to get the index of the first element of a list that matches its inpu
t and
--count(), to get the number of times an element appears in a list
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Print out the index of the element 20.0
print(areas.index(20.0))
# Print out how often 14.5 appears in areas
print(areas.count(14.5))
OUTPUT:
2
0
--append(), that adds an element to the list it is called on,
--remove(), that removes the first element of a list that matches the input, and
--reverse(), that reverses the order of the elements in the list it is called on
---------------------
PACKAGES
ou can think of package as a directory of Python
scripts. Each such script is a so-called module. These modules specify functions
, methods and
new Python types aimed at solving particular problems.
Among them are packages for data science: there's numpy to
efficiently work with arrays, matplotlib for data visualization, scikit-learn fo
r machine
learning, and many others.
//get-pip.py // to get all the packages

----
# Definition of radius
r = 0.43
# Import the math package
import math
# Calculate C
C = 2*(math.pi)*r
# Calculate A
A = (math.pi)*r**2
# Build printout
print("Circumference: " + str(C))
print("Area: " + str(A))
ANOTHER WAY TO IMPORT ONLY SPECIFIC MODULE
from math import radians
---------------------------
USING NUMPY
Numpy array is pretty similar to a regular Python list, but has one additional f
eature:
you can perform calculations over all entire arrays. It's really easy, and super
-fast as
well.
baseball = [180, 215, 210, 210, 188, 176, 209, 200]
# Import the numpy package as np
import numpy as np
# Create a Numpy array from baseball: np_baseball
np_baseball=np.array(baseball)
# Print out type of np_baseball
print(type(np_baseball))
-------------------
NUMPY WITH BOOLEAN
bmi = np_weight_kg / np_height_m ** 2
# Create the light array
light=bmi<21
# Print out light
print(light)
# Print out BMIs of all baseball players whose BMI is below 21
print(bmi[light])
-----------------------------
EXCEPTION WITH NUMPY
Numpy arrays cannot contain elements with different types. If you try to build s
uch a list, some of the elments' types are changed to end up with a homogenous l
ist. This is known as type coercion.
Second, the typical arithmetic operators, such as +, -, * and / have a different
meaning for regular Python lists and Numpy arrays.
In [1]: np.array([True, 1, 2]) + np.array([3, 4, False])
Out[1]: array([4, 5, 2])
------------------------------------
CREATE 2D NUMPY ARRAY
baseball = [[180, 78.4],
[215, 102.7],
[210, 98.5],
[188, 75.2]]
# Import numpy
import numpy as np
# Create a 2D Numpy array from baseball: np_baseball
np_baseball=np.array(baseball)
# Print out the type of np_baseball
print(type(np_baseball))
# Print out the shape of np_baseball
print(np_baseball.shape)
OUTPUT:
<class 'numpy.ndarray'>
(4, 2)
# Print out the 50th row of np_baseball
print(np_baseball[49,:]) // The row and column start with index 0
-----------------------------------------------
USING NUMPY STASTICS FUNCTIONS
# Import numpy
import numpy as np
# Print mean height (first column)
avg = np.mean(np_baseball[:,0])
print("Average: " + str(avg))
# Print median height. Replace 'None'
med = np.median(np_baseball[:,0])
print("Median: " + str(med))
# Print out the standard deviation on height. Replace 'None'
stddev = np.std(np_baseball[:,0])
print("Standard Deviation: " + str(stddev))
# Print out correlation between first and second column. Replace 'None'
corr = np.corrcoef(np_baseball[:,0],np_baseball[:,1])
print("Correlation: " + str(corr))
------------------------------------
MATPLOTLIB
With matplotlib, you can create a bunch of different plots in Python. The most b
asic plot is the line plot. A general recipe is given here.
import matplotlib.pyplot as plt
plt.plot(x,y)
plt.show()
-----------------------------
print(year[-1])
print(pop[-1])
# Import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
# Make a line plot: year on the x-axis, pop on the y-axis
plt.plot(year,pop)
plt.show()
-------------------------------------
SCATTER PLOT
# Change the line plot below to a scatter plot
plt.scatter(gdp_cap, life_exp)
# Put the x-axis on a logarithmic scale
plt.xscale('log')
# Show plot
plt.show()
------------------------
HISTOGRAM
# Create histogram of life_exp data
plt.hist(life_exp)
# Display histogram
plt.show()
------------
Building histogram with bins
# Build histogram with 5 bins
plt.hist(life_exp,bins = 5)
# Show and clean up plot
plt.show()
plt.clf()
# Build histogram with 20 bins
plt.hist(life_exp,bins=20)
# Show and clean up again
plt.show()
plt.clf() // To clear
---------------------------
CUSTOMIZATION
# Basic scatter plot, log scale
plt.scatter(gdp_cap, life_exp)
plt.xscale('log')
# Strings
xlab = 'GDP per Capita [in USD]'
ylab = 'Life Expectancy [in years]'
title = 'World Development in 2007'
# Add axis labels
plt.xlabel(xlab)
plt.ylabel(ylab)
# Add title
plt.title(title)
# After customizing, display the plot
plt.show()
-----------
TO Change THE VALUES ON y AXIS AND X AXIS
plt.yticks([0,1,2], ["one","two","three"])
In this example, the ticks corresponding to the numbers 0, 1 and 2 will be repla
ced by one, two and tree, respectively.
plt.xscale('log')
plt.xlabel('GDP per Capita [in USD]')
plt.ylabel('Life Expectancy [in years]')
plt.title('World Development in 2007')
# Definition of tick_val and tick_lab
tick_val = [1000,10000,100000]
tick_lab = ['1k','10k','100k']
# Adapt the ticks on the x-axis
plt.xticks(tick_val,tick_lab)
------------
TO ADD COLOUR AND MAKE IT A BIT TRANSPARENT
plt.scatter(x = gdp_cap, y = life_exp, s = np.array(pop) * 2,c=col,alpha=0.8)
Here col is colour given to each continent
Alpha can be set from zero to one, where zero totally transparant, and one is no
t transparant.
----------------
plt.text(1550, 71, 'India') // Add text to graph at the x,y axis specified
plt.text(5700, 80, 'China')
# Add grid() call
plt.grid(True) // To add grid
-------------------------------------------------
USE OF BOOLEAN
# Comparison of booleans
print(True == False)
# Comparison of integers
print((-5*15)!=75)
# Comparison of strings
print("pyscript"=="PyScript")
# Compare a boolean with an integer
print(True==1)
OUTPUT:
False
True
False
True
-------------
AND OR
print(my_kitchen>10 and my_kitchen<18)
# my_kitchen smaller than 14 or bigger than 17?
print(my_kitchen<14 or my_kitchen>17)
-------------
IF ELSE ELIF
# if-elif-else construct for area
if area > 15 :
print("big place!")
elif area>10:
print("medium size, nice!")
else :
print("pretty small.")
------------------------------------------------------
PANDAS
As a data scientist, you'll often be working with tons of data. You already know
about
the 2D numpy array, to house data in a rectangular structure. But there's a down
side to this:
you can only have data of the same type in there. In practice, you'll be working
with
data of different types: numerical values, strings, booleans and so on.
To easily and efficiently handle this data, there's the Pandas package. Pandas i
s a high
level data manipulation tool, used by data scientists all over the world.
In pandas, we store data in a so-called dataframe.
# Import pandas as pd
import pandas as pd
# Import the cars.csv data: cars
cars = pd.read_csv('cars.csv',index_col=0) // Define path
# Print out cars
print(cars)
OUTPUT:
cars_per_cap country drives_right
US 809 United States True
AUS 731 Australia False
JAP 588 Japan False
IN 18 India False
RU 200 Russia True
MOR 70 Morocco True
EG 45 Egypt True
-----------
SERIES AND DATAFRAME
cars['cars_per_cap']
cars[['cars_per_cap']]
The single bracket version gives a Pandas Series, the double bracket version giv
es a Pandas DataFrame.
print(cars.loc['RU'])
OUTPUT:
cars_per_cap 200
country Russia
drives_right True
print(cars.loc[['RU']])
OUTPUT:
Name: RU, dtype: object
cars_per_cap country drives_right
RU 200 Russia True
---------
PRINTING ROWS AND COLUMNS
# Print out drives_right value of Morocco
print(cars.loc['MOR','drives_right'])
True
# Print sub-DataFrame
print(cars.loc[['RU','MORcl'],['country','drives_right']])
OUTPUT:
country drives_right
RU Russia True
MOR Morocco True

Вам также может понравиться