Introduction To Python Part 2

Jreyes
2 min readNov 2, 2021

--

Let's continue where we left off.

String:

The string data type is used to represent characters or alphabets. You can declare a string using a single ” or double quotes “”.

name = 'Jose'course = "python"

To access the values in a string, we can use indexes.

name = 'Jose'
name[2] ---> 's'
# the output will be the alphabets at that particular index.Remember that the index starts at 0

List:

List in python is like a collection where you can store different values. It need not be uniform and can have different values.

Lists are indexed and can have duplicate values as well. To declare a list, you have to use square brackets.

my_list = [10, 20, 30, 40, 50, 60]
print(my_list)

To access values in a list we use indexes, following are a few operations that you can perform on a list:

  • append
  • clear
  • copy
  • count
  • extend
  • insert
  • pop
  • reverse
  • remove
  • sort

Following is a code for a few operations using a list:

a = [10,20,30,40,50]#append will add the value at the end of the lista.append('Jose')#insert will add the value at the specified indexa.insert(2,'eduardo')#reverse will reverse the lista.reverse()print(a)#the output will be['Jose', 50, 40, 30, 'eduardo', 20, 10]

Dictionary

A dictionary is unordered and changeable, we use the key-value pairs in a dictionary. Since the keys are unique we can use them as indexes to access the values from a dictionary.

Following are the operations you can perform on a dictionary:

  • clear
  • copy
  • fromkeys
  • get
  • items
  • keys
  • pop
  • getitem
  • setdefault
  • update
  • values
my_dictionary = { 'key1' : 'jose' , 2 : 'python'}mydictionary['key1']#this will get the value 'Jose'. the same purpose can be fulfilled by get().my_dictionary.get(2)#this will get the value 'python'.

Tuple

A tuple is another collection that is ordered and unchangeable. We declare the tuples in python with round brackets. Following are the operations you can perform on a tuple:

  • count
  • index
mytuple = (10,20,30,40,50,50,50,60)mytuple.count(40)#this will get the count of duplicate values.mytuple.index(20)#this will get the index for the vale 20.

Set

A set is a collection that is unordered and unindexed. A set does not have any duplicate values as well. Following are some operations you can perform on a set:

  • add
  • copy
  • clear
  • difference
  • difference_update
  • discard
  • intersection
  • intersection_update
  • union
  • update
myset = { 10 ,20,30,40,50,60,50,60,50,60}print(myset)#there will be no duplicate values in the output

In any programming language, the concept of operators plays a vital part. Let's take a look at operators in python in the next post. See you next week.

--

--

Jreyes
Jreyes

Written by Jreyes

I'm a software engineer making his way into the tech industry.

No responses yet