No Fluff Guide to Python - P2 - Working with Data

 Data is everything

 Code alone does very little. It needs Data to work on. Anything you hear or see is data.

Some common examples of data:

  • list of names
  • a set of values
  • one name
  • 7 numbers
  • xml data
  • csv file
  • json string
  • url of a website
  • html body of a webpage
  • txt file on your laptop

 

Ok! let's start coding.


What do we have in Python to handle all these forms of data?

LIST

 Here's a list of operating systems

list = ["windows", "mac", "linux"]
#Lists are mutable

List is enclosed within [ ]

We can add and remove values from a list.

List can be heterogeneous. It can contain diverse content

list1=["hello",1,4,8,"good"]

List1 contains strings and numbers

List is

  • ordered
  • accessed by indexes
list2=["red","green","blue","yellow","black"]

Python List - Learn By Example

Starting from the 1st element, they are given index of 0 and onwards.

So, 5th element will have an index on 4.

 

You can access the 3rd element in the list using the index 2:

list1[2]

>>> blue


You can change the value at an index by:

list1[0]="purple"  
# assigning values
# "red" is replaced with "purple")


You can merge and display 2 lists together:

print(list1+list2) 
# list concatenation

 

Add and remove values from a list:

list1=["Ducati v4","Harley CVO","Kawasaki Ninja"]
list1.append("Hayabusa")   

# Hayabusa is added to the list
print
(list1)

list1.pop()  # removes the last element from the list print(list1) #Output #['Ducati v4', 'Harley CVO', 'Kawasaki Ninja', 'Hayabusa'] #['Ducati v4', 'Harley CVO', 'Kawasaki Ninja']

 

Here's a quick overview of all properties of a list:


 

What if I don't want my data in the List to change?

TUPLE:

They are very much identical to lists, however, items inside tuple are constant. For example, storing some constant data in a program.

tuple = ("linux", "mac")
#Tuples are immutable
print(tuple1[0])  # accessing values using indexing
 
#tuple1[1]="change"  # a value cannot be changed as they are immutable

 Updates using index, pop, append doesn't work on tuple.


Lists seems fine if I want to store similar kind of data. 

What can I use if I want to store, lets say user data:

  • first name
  • last name
  • phone number, etc

DICTIONARY:

dict = {"name" : "vish", "age" : 30}
#Dictionaries are mutable and keys do not allow duplicates

Instead of indexes, you access the values using KEYS.

The data is stored in KEY: VALUE format.

dict1={"key1":1,"key2":"value2",3:"value3"}
print(dict1.keys())  

# all the keys are printed

#Output #dict_keys(['key1', 'key2', 3])

You can access all keys using KEYS()

You can access all values using VALUES()

dict1.values()      # all the values are printed
dict1["key1"]="replace_one"    # value assigned to key1 is replaced
print(dict1)
print(dict1["key1"])

#Output #{'key1': 'replace_one', 'key2': 'value2', 3: 'value3'} #replace_one

 

Update a dictionary with new value:

salad={"fruit1":"apple","fruit2":"banana","veg1":"tomato"}
salad.update({"veg2":"chilli"})  # updates the dictionary at the end
print(salad)


salad.pop("veg2") #remove veg2 print(salad) #Output #{'fruit1': 'apple', 'fruit2': 'banana', 'veg1': 'tomato', 'veg2': 'chilli'} #{'fruit1': 'apple', 'fruit2': 'banana', 'veg1': 'tomato'}

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

Index Clean-Up Scripts

forgot sa password and no logins are added

The SQL Server DBA’s Guide to Teradata