中级农民
- 积分
- 101
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-1-19
- 最后登录
- 1970-1-1
|
day1 10/20
• ** #exponent
• = #assign
• Numerical values like integers and floats don't require quotation marks, but strings do.
• type() #won't display anything. Instead, it will return the data type as a value, which can be assigned to another variable or displayed using the print() statement
hello = 'Hello'
print(type(hello))
• Make a list:
Variablename = [] #create an empty list, then
list.append() #method to add values to a list object
或者
cities = ["Albuquerque", "Anaheim", "Anchorage", "Arlington", "Atlanta"]
• Unlike functions, methods are called using dot notation (.) on a specific object.
• A list starts at index 0 and goes all the way to one less than the total number of values
crime_rates = [749, 371, 828, 503, 1379]
first_value = crime_rates[0]
second_value = crime_rates[1]
fifth_value = crime_rates[4]
• len() #function returns the length of a list, or the number of elements in that list.
• Slicing:
The value at the starting index and all of the values in between will be returned. The value at the ending index will not.
crime_rates = [749, 371, 828, 503, 1379]
# The following slice selects values at index 2 and 3, but not 4.
two_four = crime_rates[2:4]
ending_index = len(crime_rates)
two_to_end = crime_rates[2:ending_index]
• a = open("story.txt", "r")
##r means read. a doesn't contain the actual content
• f = open("test.txt", "r")
g = f.read()#g contains the actual content
• Splitting:
\n #换行
Albuquerque,749\nAnaheim,371\nAnchorage,828\n
split()#method to turn a string object into a list of strings
sample = "john,plastic,joe"
split_list = sample.split(",")# split_list is a list of _strings_: ["john", "plastic", "joe"]
split_list = sample.split("\n")
• Loop:
cities = ["Austin", "Dallas", "Houston"]
for city in cities:
print(city)
#注意loop body之前有冒号,loop body前空4个空格
• List of lists
three_rows = ["Albuquerque,749", "Anaheim,371", "Anchorage,828"]
final_list = []
for row in three_rows:
split_list = row.split(',')
final_list.append(split_list)
print(final_list)
# the elements in final_list are lists.
Here's what the output looks like:
[['Albuquerque', '749'], ['Anaheim', '371'], ['Anchorage', '828']]
• # Returns the first list's first element, 'Albuquerque'.
first_list_first_value = final_data[0][0]
# Returns the second list's first element, 'Anaheim'.
second_list_first_value = final_data[1][0]
# Returns the third list's first element, 'Anchorage'.
third_list_first_value = final_data[2][0]
|
|