This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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(崩溃). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,最后输出新序列 |
NewerOlder