SoFunction
Updated on 2024-10-28

python for loop inner and outer output methods

Summing by for loop, it turns out that the output is completely different, one loop is outputting the result of each step, the other loop is outputting the result of the final one, finally figured it out today.

As shown below:

Addendum: two ways to output (index,value) in a for loop in python

index

value index value

Method I. Utilizing enumerate()

list1=['a','s','d','g']
for index,value in enumerate(list1):
 print('Index: %d,index value: %s'%(index,value))

Output results:

Index: 0,Index value: a

Index: 1,Index value: s

Index: 2, Index value: d

Index: 3, Index value: g

Method II. Use of range(len())

list1=['q','w','e','r']
for i in range(len(list1)):
 print('Index: %d,index value: %s'%(i,list1[i]))

Output results:

Index: 0,Index value: q

Index: 1,Index value: w

Index: 2, Index value: e

Index: 3, Index value: r

Addendum: Python's most basic syntax (input/output, for loops)

Python - Getting Started in Getting Started - Printing Calendars

History:

The first half of freshman year C++ book case, even the comments with the code a total of two hundred lines, when their own C++ wrote a full 200 lines to solve. Then on New Year's Day 19, I picked up python and rewrote the problem, writing 60 lines. In order to recall the python syntax rewrite again, the results of writing found that only thirty lines of the same (I blame the reinstallation of the system forgot to backup the python file, so now I do not even know how to write so long at that time)

main idea of the question

Knowing that January 1, 2000 is a Saturday, calculate and print the calendar according to the date (as shown)

coding

def get_year_days(n):
 if n%4==0 and n%100!=0 or n%400==0 : return 366
 else : return 365
def get_month_days(n,m):
 if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:return 31
 if m == 4 or m == 6 or m == 9 or m == 11 : return 30
 if m == 2:
 if n==365: return 28
 else:return 29
target_n = int(input("Please input year"))
target_y = int(input("Please input month"))
sum = 0
for x in range(2000,target_n):
 print(x,get_year_days(x))
 sum = sum + get_year_days(x)
for x in range(1,target_y):
 print(x,get_month_days(get_year_days(target_n),x))
 sum = sum + get_month_days(get_year_days(target_n),x)
# print(sum)
dx = (sum+6)%7
if dx == 0: dx = 7
print("One, two, three, four, five, six days.")
for x in range(1,dx+1):print(" ",end = " ")
for x in range(1,get_month_days(get_year_days(target_y),target_y)):
 if x<=9 and x>=1 : print(x , end = " ")
 else :print(x,end = " ")
 if (x+dx)%7==0:print()

Organize Grammar Points of Error

I/O format

(I haven't written python in four months, so I've really forgotten all about it, and it took me a while to find out how to input and output it.)

1, understand input, it can accept many forms of input, if you want to determine his data type when stored, you have to use a bracket around and then write the data type in front.

a = input(); # At this point the input is stored in an indeterminate form in a
a = int(intput()); # At this point what is typed is automatically stored as an int data type in a
a = int(input("One sentence.")) #At this point the output is preceded by the output of"One sentence."

2. Output format

It's print, not println nor printf (influenced by recent habits of writing Java and typing acm) thought println could do line breaks, only to realize that print itself can do line breaks. There is also a way not to break lines, which is to add an end = ...... to print's () to change the way the output is processed.

print(10) # Output 10
print(a) # Output a
print(a , end = " ") # End this output with a space
print(a, end = "Anything.") #in order to"Anything."to conclude this output

Ranges for for loops

The traversal of range is front-open and back-open (expressed as an interval)

for x in range(1,5) : #This iterates through the1 2 3 4,It won't be traversed.5

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.