Featured Posts

CS50 Python , Nutrition Facts Table

Image
Nutrition Facts: Python Practice for Beginners Nutrition Facts Table for Python Practice Welcome to this comprehensive guide for Python beginners! If you are learning how to work with lists, dictionaries, and loops, this post will help you build practical skills using a real-world example: nutrition facts for fruits. Understanding how to organize and manipulate data is a key part of programming, and this exercise will give you hands-on experience. Below is a sample table of fruits and their calorie values, formatted as a Python list of dictionaries. This structure is ideal for coding exercises, projects, or even building your own nutrition calculator. You can expand this list, add new fruits, or use it as a foundation for more advanced Python tasks. Python List of Dictionaries Example: fruits = [ {'name': 'Apple', 'calories': 130}, {'name': 'Avocado', 'calories': 50}, {'name': 'Banana', 'ca...

Python 3.6.1.9 LAB: Operating with lists - basics

Python 3.6.1.9 LAB: Operating with lists - basics

3.6.1.9 LAB
3.6.1.9 LAB

If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.6.1.9 LAB: Operating with lists - basics.

Objectives

Familiarize the student with:

  • list indexing;
  • utilizing the in and not in operators.

Scenario

Imagine a list - not very long, not very complicated, just a simple list containing some integer numbers. Some of these numbers may be repeated, and this is the clue. We don't want any repetitions. We want them to be removed.

Your task is to write a program which removes all the number repetitions from the list. The goal is to have a list in which all the numbers appear not more than once.

Note: assume that the source list is hard-coded inside the code - you don't have to enter it from the keyboard. Of course, you can improve the code and add a part that can carry out a conversation with the user and obtain all the data from her/him.

Hint: we encourage you to create a new list as a temporary work area - you don't need to update the list in situ.

We've provided no test data, as that would be too easy. You can use our skeleton instead.

Solution Code

my_list = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
new_list = [] # The helping list inside which we will add only unique numbers

for i in my_list:
    if i not in new_list: # The comparison part
        new_list.append(i) # Only ubique numbers will be added. Duplicates wont.
my_list = new_list # To update the original list. Or, you can print the new_list
                     instead.

print("The list with unique elements only:")
print(my_list)

And here's what is happening inside the code:

=================================================================================

Follow the Python page on the Blog to be the first to know

Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst