Skip to Main Content

Introduction to Python

This guide was designed to supplement the INTRODUCTION TO PYTHON virtual workshop. If you have concerns or suggestions for this guide, feel free to reach out to scholarlyengagement@tulane.edu.

Video

What are methods?

Methods are special operations that can be used on specific data types. Methods are attached to the end of variables.

Variable.method()

In this section, you will learn how to:

  • Use common list, string, number, and dictionary methods
  • Identify Python's indexing logic

Common List Methods

Copy the following code on the left in your IDE to execute the process shown in the image on the right.


Adding Elements to a List                                                        

list1 = ["you", "me", "us", "we"]                                          

list1.append("them")

print(list1)


Removing Elements from a List

Before using this method, you must first understand the indexing structure of Python. When creating a complex variable like a list, Python gives each of the variable elements a number to represent its place in the variable. These numbers begin at 0. For example, in the list shown above, "you" has an index of 0 while "me" has an index of 1. 

list1 = ["you", "me", "us", "we"]                                                               
list1.pop(0)
print(list1)

 

You can also remove a list element by referencing the element itself. 

list1 = ["you", "me", "us", "we"]                                                                 
list1.remove("us")
print(list1)

 


Finding an Element's Index

list1 = ["you", "me", "us", "we"]                                                                
meindex= list1.index("me")
print(meindex)

 


Counting Elements in a List

list1 = ["you", "me", "us", "we"]                                                                     
wecount=list1.count("we")
print(wecount)

 


Join Lists

list1 = ["you", "me", "us", "we"]                   
list2 = ["Sheila", "Joe", "Ronald", "Jacob"]     

list1.extend(list2)
print(list1)

 

 


Alphabetically Sort a List

list1 = ["you", "me", "us", "we"]                                                                   
list1.sort()
print(list1)

Common String Methods

Copy the following code on the left in your IDE to execute the process shown in the image on the right.


Capitalizing the First Word of a String

string = "Tulane University Libraries"                            
string=string.capitalize()
print(string)

 

 


Capitalizing the First Letter of Each Word

string = "Tulane university libraries"                              
string string.title()
print(string)

 

 


Making All Letters Lowercase

string = "Tulane University Libraries"                   
string = string.lower()
print(string)

 

 


Making All Letters Uppercase

string = "Tulane University Libraries"                                
string = string.upper()
print(string)

 

 


Finding the Index of a Letter (First Occurrence Only/Case Sensitive)

string = "Tulane University Libraries"                   
findU= string.find("U")
print(findU)

 

 


Creating a List of Words in a String

string = "Tulane University Libraries"
stringlist = string.split(" "
print(stringlist)                                                 

Common Dictionary Methods

Copy the following code on the left in your IDE to execute the process shown in the image.


Getting the Keys of a Dictionary

dct = {"shoe": 8, "shirt": "medium", "wpants": 34, "lpants": 32}         
dct.keys()             

                                 

 

 

 

 


Getting the Values of a Dictionary

dct = {"shoe": 8, "shirt": "medium", "wpants": 34, "lpants": 32}     

dct.values()                  

                                             

 

 

 


Getting the Key-Value Pairs of a Dictionary

dct = {"shoe": 8, "shirt": "medium", "wpants": 34, "lpants": 32}     

dct.items()                                  

 


Getting a Value by Providing a Key

dct = {"shoe": 8, "shirt": "medium", "wpants": 34, "lpants": 32}     

dct.get("shoe")             

                                                

 

 

 

 

 


Delete a Key-Value Pair from a Dictionary

dct = {"shoe": 8, "shirt": "medium", "wpants": 34, "lpants": 32

dct.pop("shoe")
print(dct) 

                                            

 

 

 


Add a Key-Value Pair to a Dictionary

dct = {"shoe": 8, "shirt": "medium", "wpants": 34, "lpants": 32

dct.update({"hat":"small"})
print(dct)

              

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.