Skip to main content

Command Palette

Search for a command to run...

Python Basics. Part 1.

Published
3 min read

Types of data in Python: float, int, string and boolean (True or False)

They can be combined in lists.

You can access information from the list using the so-called index. Index count starts from 0 => zero-indexing You can also count backwards, using negative indexes. It is useful for getting information from the end of the list:

x = ["a", "b", "c", "d"]
x[1] x[-3] #same result!

List Slicing allows to select multiple elements from the list, thus creating a list => by creating a colon. Example fam[3:5].

The index, which is specified before the colon ":" is included, and the index after ":" is excluded.

[start:end] => start is included, end is excluded.

# Example of list: 
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

# Create list areas
areas = [hall, kit, liv, bed, bath]

# Print areas
print(areas)

<script.py> output:
[11.25, 18.0, 20.0, 10.75, 9.5]
#--------------------------------------------------------------------------------------------
#------------------------------WORKING_WITH_LISTS--------------------------------------------
# Adapt list areas
areas = ['hallway', hall, 'kitchen', kit, "living room", liv, 'bedroom', bed, "bathroom", bath]

# Print areas
print(areas)

<script.py> output:
['hallway', 11.25, 'kitchen', 18.0, 'living room', 20.0, 'bedroom', 10.75, 'bathroom', 9.5]
#--------------------------------------------------------------------------------------------
# House information as list of lists
house = [["hallway", hall],
         ["kitchen", kit],
         ["living room", liv],
         ['bedroom', bed],
         ["bathroom", bath]]

# Print out house
print(house)

<script.py> output:
[['hallway', 11.25], ['kitchen', 18.0], ['living room', 20.0], ['bedroom', 10.75], 
['bathroom', 9.5]]
#--------------------------------------------------------------------------------------------
#-------------------------------------LIST_SLICING-------------------------------------------

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]

# Use slicing to create downstairs
downstairs = areas[0:6]

# Use slicing to create upstairs
upstairs = areas[-4:]

# Print out downstairs and upstairs
print(downstairs)
print(upstairs)
['hallway', 11.25, 'kitchen', 18.0, 'living room', 20.0]
['bedroom', 10.75, 'bathroom', 9.5]

<script.py> output:
    ['hallway', 11.25, 'kitchen', 18.0, 'living room', 20.0]
    ['bedroom', 10.75, 'bathroom', 9.5]
#--------------------------------------------------------------------------------------------
#Subsetting list of lists: 

house = [["hallway", 11.25],
         ["kitchen", 18.0],
         ["living room", 20.0],
         ["bedroom", 10.75],
         ["bathroom", 9.50]]

# Subset the house list
house[-1][1] #not clear what this string is for
house[4][-1]

<script.py> output:
9.5
#-------------------------------------------------------------------------------------------
#-----------------------------------LIST MANUPULATION---------------------------------------
#Replacing list elements

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]

# Correct the bathroom area
areas[-1] = 10.50

# Change "living room" to "chill zone"
areas[4] = "chill zone"
#--------------------------------------------------------------------------------------------
#Extending a list

# Create the areas list and make some changes
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
         "bedroom", 10.75, "bathroom", 10.50]

# Add poolhouse data to areas, new list is areas_1
areas_1 = areas + ["poolhouse", 24.5]

# Add garage data to areas_1, new list is areas_2
areas_2 = areas_1 + ["garage", 15.45]
#--------------------------------------------------------------------------------------------
#Delete list elements

areas = ["hallway", 11.25, "kitchen", 18.0,
        "chill zone", 20.0, "bedroom", 10.75,
         "bathroom", 10.50, "poolhouse", 24.5,
         "garage", 15.45]

# Delete the poolhouse items from the list
del areas[-4:-2]

# Print the updated list
print(areas)

<script.py> output:
['hallway', 11.25, 'kitchen', 18.0, 'chill zone', 20.0, 'bedroom', 10.75, 
 'bathroom', 10.5, 'garage', 15.45]
#--------------------------------------------------------------------------------------------
#Inner working with lists

# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]

# Change this command
areas_copy = areas[:] #changes in 'areas_copy' do not affect 'areas' 

# Change areas_copy
areas_copy[0] = 5.0

# Print areas
print(areas)

<script.py> output:
    [11.25, 18.0, 20.0, 10.75, 9.5]