Coding Exercise for Beginners in Python |Exercise 14 | Python Tutorials for Beginners #lec44

Coding Exercise for Beginners in Python |Exercise 14 | Python Tutorials for Beginners #lec44

How to Calculate Average Height Using Python

Introduction to the Exercise

  • The video introduces a coding exercise focused on calculating the average height from a list of heights using a for loop.
  • A sample list of heights is provided, illustrating that while the task seems simple, there are specific constraints to follow.

Constraints and Requirements

  • Participants cannot use built-in functions like sum and len; they must replicate these functionalities using for loops.
  • User input must be taken through the input function, emphasizing understanding its properties and functionality.
  • The output should be rounded to the nearest whole number, highlighting three key requirements: no built-in functions, user input handling, and rounding.

Functions to Use

  • The split function is essential for converting user input (a string) into a list of heights by separating values based on spaces.
  • The range function will help generate sequences of numbers; it starts at 0 by default but can be customized with specified start and stop points.

Implementation Steps

  • Viewers are encouraged to pause the video and attempt the exercise independently before reviewing the solution.
  • The instructor begins coding by taking user input for heights with an explanation of how this data will initially be treated as a string.

Processing Input Data

  • After receiving input, the string needs to be split into individual height values using the split method.
  • An example illustrates splitting based on spaces; if commas were used instead, adjustments would need to be made in specifying separators.
  • A variable named height_list is created to store these split values as a list. However, it's noted that these values remain strings until further processing occurs.

How to Convert a List of Strings to Integers

Understanding Type Conversion in Python

  • The speaker discusses the need to convert a list of strings into integers, emphasizing that type casting is necessary for this operation.
  • An error occurs when attempting to directly convert a list using int(), as it requires a string, byte-like object, or real number—not a list.

Traversing the List

  • To convert each item in the list, one must traverse through it. The speaker introduces the use of a for loop with an index variable.
  • The speaker explains how to access elements in the list using their indices but highlights that using string values as indices will lead to errors.

Using Range Function for Indexing

  • A suggestion is made to utilize the range() function for iterating over indices from 0 up to the length of the list, which should be dynamically determined rather than hardcoded.
  • The speaker proposes initializing a counter variable (count) at zero and incrementing it within another loop that traverses through each element of the height list.

Calculating Length Without len()

  • By iterating through each element and incrementing count, one can determine the length of the list without using Python's built-in len() function.
  • After completing this process, printing count confirms its value reflects the actual number of items in the height list.

Finalizing Integer Conversion and Summation

  • With an established count, one can now iterate from 0 up to this count and convert each string in height_list into an integer.
  • The conversion process is reiterated with examples showing how integers are assigned back into their respective positions within height_list.

Summation Process Introduction

  • Finally, after confirming all elements are converted successfully, there’s an introduction to calculating their sum by initializing a sum variable and preparing for traversal through the updated integer list.

Understanding Average Calculation in Python

Looping Through a List to Calculate Sum

  • The process begins with a simple loop to traverse through a list of heights, using any variable name (e.g., person) for each item in the list.
  • The sum is updated by adding each person's height to the total using either sum = sum + person or shorthand sum += person.

Calculating Average from Total Height

  • To find the average height, a new variable AVG is introduced, calculated as total / count, where total represents the cumulative height.
  • It’s important to avoid naming variables after built-in functions like sum; hence, total is used instead.

Rounding and Displaying Results

  • After calculating the average, it’s rounded to the nearest whole number using Python's built-in round function. For example, an average of 154.6 would be rounded to 155.
  • The program outputs both the count of heights entered and the rounded average height.

Practical Application and Understanding Key Functions

  • Users are encouraged to practice coding by writing down examples on paper and performing dry runs for better understanding.
  • The final output can be formatted for clarity; for instance, displaying "Average height is 155" enhances readability.

Key Concepts Highlighted

  • Understanding how to use loops effectively in Python.
  • Importance of avoiding conflicts with built-in function names when creating variables.
  • Utilizing rounding functions for cleaner output results.
Video description

Python program to calculate average height from a list of heights ( Don't use sum() and len() functions and take use input using input() function) hint: use split() and range() functions Best Python Tutorials for Beginners: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bZSiqiOL5ta39vSnBxpOPT ********************************************* Connect & Contact Me: My Second Channel Link: https://bit.ly/354n7C7 Facebook: https://www.facebook.com/Jennys-Lectures-CSIT-Netjrf-316814368950701/ Quora: https://www.quora.com/profile/Jayanti-Khatri-Lamba Instagram: https://www.instagram.com/jayantikhatrilamba/ Twitter: https://twitter.com/KhatriJenny ******************************************* More Playlists: Programming in C Tutorials: https://www.youtube.com/playlist?list=PLdo5W4Nhv31a8UcMN9-35ghv8qyFWD9_S C++ Tutorials for beginners: https://www.youtube.com/watch?v=KYxLEDF6kjs&list=PLdo5W4Nhv31YU5Wx1dopka58teWP9aCee Placement Series: https://www.youtube.com/playlist?list=PLdo5W4Nhv31YvlDpJhvOYbM9Ap8UypgEy Data Structures and Algorithms: https: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU Design and Analysis of Algorithms(DAA): https://www.youtube.com/playlist?list=PLdo5W4Nhv31ZTn2P9vF02bkb3SC8uiUUn Dynamic Programming: https://www.youtube.com/playlist?list=PLdo5W4Nhv31aBrJE1WS4MR9LRfbmZrAQu Operating Systems tutorials: //www.youtube.com/playlist?list=PLdo5W4Nhv31a5ucW_S1K3-x6ztBRD-PNa DBMS Tutorials: https://www.youtube.com/playlist?list=PLdo5W4Nhv31b33kF46f9aFjoJPOkdlsRc #coding #pythonforbeginners #python #jennyslectures #pythonprogramming #loop