Skip to content

Instantly share code, notes, and snippets.

@ChuckleQu
ChuckleQu / 25_Day_Exercise.py
Created September 29, 2020 09:05
25_Day_Pandas
import pandas as pd
df = pd.read_csv('hacker_news.csv')
# 1
first_five_rows = df.head()
print(first_five_rows)
print()
# 2
last_five_rows = df.tail()
@ChuckleQu
ChuckleQu / 24_Day_Numpy_and_Statistics.py
Created September 26, 2020 09:19
24_Day_Python_for_Statistical_Analysis
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import calendar
# Numpy and Statistics
normal_array = np.random.normal(79, 15, 80)
# create a 80 numbers array forming normal distribution with μ = 79 and σ = 15
@ChuckleQu
ChuckleQu / 22_Day_Web_Scraping.py
Created September 16, 2020 14:19
22_Day_Web_Scraping
import requests
from bs4 import BeautifulSoup
url = 'https://visa.educationmalaysia.gov.my/guidelines/travel-authorisation-form-base.html'
# Lets use the requests get method to fetch the data from url
response = requests.get(url)
# lets check the status
status = response.status_code
@ChuckleQu
ChuckleQu / 21_Day_Classes_and_Objects.py
Created September 16, 2020 14:18
21_Day_Classes_and_Objects
# Creating a Class
# To create a class we need the key word class followed by the name and colon. Class name should be CamelCase.
# syntax
# class ClassName:
# code goes here
class Person:
pass
# Creating an Object
import requests
import re
# 1
Romeo_and_juliet = 'http://www.gutenberg.org/files/1112/1112.txt'
response = requests.get(Romeo_and_juliet)
text = response.text
def find_most_common_words(text, n):
@ChuckleQu
ChuckleQu / 19_Day_Exercise_1_2_3_4.py
Created September 16, 2020 14:15
19_Day_File_Handling
import json
import re
# 1
def number_of_lines(txt):
with open(txt, 'r', encoding='UTF-8') as file:
lines = file.read().splitlines()
num_of_lines = len(lines)
return num_of_lines
@ChuckleQu
ChuckleQu / 18_Day_Exercise.py
Created September 16, 2020 14:14
18_Day_Regular_Expressions
import re
# 1
paragraph = 'I love teaching. If you do not love teaching what else can you love. I love Python if you do not love ' \
'something which can give you all the capabilities to develop an application what else can you love.'
match1 = re.split(r' |\. ', paragraph)
paragraph_set = set(match1)
@ChuckleQu
ChuckleQu / 17_Day_Exception_Handling_1.py
Created September 16, 2020 14:13
17_Day_Exception_Handling
# Python uses try and except to handle errors gracefully.
# A graceful exit (or graceful handling) of errors is a simple programming idiom(习语) -
# a program detects(检测) a serious error condition and "exits gracefully", in a controlled manner as a result.
# Often the program prints a descriptive(描述性的) error message to a terminal or log(日志) as part of the graceful exit,
# this makes our application more robust(健壮的;稳健的).
# The cause of an exception(例外) is often external to the program itself.
# An example of exceptions could be an incorrect input, wrong file name, unable to find a file,
# a malfunctioning(出故障的) IO(Input Output) device.
# Graceful handling of errors prevents our applications from crashing(崩溃).
@ChuckleQu
ChuckleQu / 16_Day_Exercise.py
Created September 16, 2020 14:12
16_Day_Python_Date_Time
from datetime import datetime, date
# 1
now = datetime.now()
print(now)
day = now.day
month = now.month
year = now.year
hour = now.hour
minute = now.minute
@ChuckleQu
ChuckleQu / 14_Day_Exercise.py
Created September 16, 2020 14:11
14_Day_Higher_order_functions
from functools import reduce
import string
countries = ['Estonia', 'Finland', 'Sweden', 'Denmark', 'Norway', 'Iceland']
names = ['Asabeneh', 'Lidiya', 'Ermias', 'Abraham']
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 1
# map(function, iterable): 对 iterable 中的每一个元素 apply function,最后输出新序列