Define a function
def square(value):
...: new_value = value ** 2
...: return new_value
Docstring(描述函数作用)
三个引号 ‘’‘xxxxxxx’’’ (在function header 和function body 之间 )
def square(value):
...: """Return the square of a value."""
...: new_value = value ** 2
...: return new_value
实例
# Import pandas
import pandas as pd
# Import Twitter data as DataFrame: df
df = pd.read_csv('tweets.csv')
# Initialize an empty dictionary: langs_count
langs_count = {}
# Extract column from DataFrame: col
col = df['lang']
# Iterate over lang column in DataFrame
for entry in col :
# If the language is in langs_count, add 1
if entry in langs_count.keys():
langs_count[entry]=langs_count[entry] +1
# Else add the language to langs_count, set the value t