Learning Blog
Lists/Arrays - December 3
Python Code
Detailed Explanation and Terminology
In this section, we define two lists, students and marks. students holds the names of the students, and marks holds their corresponding marks. These lists are used later to display the names and marks of the students.
Programming Concept: Data Structures: Lists are used here as data structures to store multiple values in an ordered collection. Lists provide an efficient way to manage related data (such as student names and marks).
Programming Concept: Initialization: The lists students and marks are initialized with values at the start of the program. This ensures that we have predefined data to work with.
This section loops through the students list using a for loop and prints each student's name.
Programming Concept: Iteration (For Loop): The for loop is used here to iterate over each element in the students list and print it. The loop runs once for every student in the list.
Programming Concept: Output: The purpose of the loop is to output each student's name sequentially.
This section uses a while loop to print each student's mark from the marks list. The loop continues until all elements in the list are printed.
Programming Concept: Iteration (While Loop): Unlike the for loop, which iterates over each element directly, the while loop here uses a counter (count) to track the current index in the marks list. This is useful when you need more control over the loop, such as incrementing a counter.
Programming Concept: Conditionals: The condition count < len(marks) ensures that the loop will run until every mark in the list is printed.
This section uses a for loop to iterate over the index of the students list. It prints both the student's name and their corresponding mark from the marks list.
Programming Concept: Iteration (For Loop with Index): The range(len(students)) function is used here to loop through the indices of the students list. The index i is then used to access both the student's name and their corresponding mark.
Here, the program calculates the average of the marks by summing the marks list using the sum() function and then dividing by the length of the marks list (the total number of students). The result is printed.
Programming Concept: Mathematical Operations: The sum(marks) and len(marks) functions are used to perform basic mathematical operations to calculate the average mark.
This section prompts the user to input the number of students they wish to add, then uses a for loop to input names and marks for each student. The entered data is stored in the new_students and new_marks lists.
Programming Concept: User Input: The input() function is used to get user input for both the number of students and their details. It allows dynamic data entry during the program's execution.
Programming Concept: Dynamic Data Storage: The lists new_students and new_marks are dynamically populated based on user input, which makes the program more interactive and flexible.
This section prints the names and marks of the students entered by the user, using a for loop to iterate through the new_students and new_marks lists.
Programming Concept: Iteration (For Loop): The loop is used to access and display the student data (names and marks) from the lists.
Programming Concept: Output: The data entered by the user is printed in a readable format.
This section calculates and displays the average of the marks entered by the user. It uses the same technique as before (sum and length of the list).
Programming Concept: Aggregation and Calculation: The sum() and len() functions are used to calculate the total and the average of the marks entered by the user.
Programming Concept: Derived Data: The average marks are derived from the entered data, showcasing how new input can be processed and summarized.
Conclusion
These terms help us understand the structure and function of a program. Mastering these terms is crucial to learning programming as they form the basic concepts and mindset of programming. In the learning blog I share, these terms can be combined to enhance the understanding and interpretation of the code.