TUPLES, FILES AND SETS IN PYTHON

From this text I’m going to talk about some more core data types used in Python. Previously we talked about the lists, dictionaries, numbers and strings too.

TUPLES

  • Tuples are same like lists and they are immutable, that they cannot be changed after created. We can simply create a tuple using ‘()‘. Also there is a ‘tuple()‘ method to convert a list into a tuple as well.

1

  • We can use slicing and other techniques we used with strings and lists with tuples as well.

2.png

3.png

  • Using the ‘index()‘ method we can get the index of a particular element  in a tuple.

4

  • Also there is a method to find out the occurrence of a particular entity in a tuple known as ‘count()‘.

5

  • As we usually do, we can remove a tuple easily by using the ‘del‘ function.

6

  • Also we can nested tuples inside another tuple like what we did in lists in making matrices.

7

8

9

  • Also if you want to get the minimum and the maximum value of a tuple, you can use the ‘min()‘ and ‘max()‘ build in methods respectively.

10

11

FILES

When we use the terminal we can give standard inputs to the program and then get the standard output to the terminal.

  • Using the ‘raw_input()‘ you can give standard inputs to a program.

20.png

When we run the program, then it will ask for a input.

21.png

Then it will take the user input and show the  output as well.

22

23

  • Also there is another method called ‘input()‘ which will take another python expression as an input and then execute it and give the output.

24

25

But according to this way we can give inputs only as the standard inputs and outputs as standard outputs. That means we have to give the inputs using the keyboard and outputs will be displayed on the terminal window. But what if we want to take input from a file or to write the output to a file. Then the file manipulation techniques will bee important.

  • First of all let’s see how to open a file. There is a inbuilt method ‘open()‘, which will take the file name and the mode as parameters, which will open/create a file. There are few modes in python file manipulation.
    • w – write data to a file
    • r – read data from a file
    • a – append data to an existing file

12

This will create a new text file at the specific location. Then it will add the data to the file. After we have finished using the file, you should close the file using ‘close()‘.

13

14

  • Mode ‘r‘ is for reading a data inside a file.

16.png

  • We can assign the data into a variable and can use it at a later time too.

17.png

  • There is a method called ‘split()‘, which will split the text of a file and then creates a list from the data in it.

18

  • Mode ‘a‘ for append data for an existing file. This will keep the old data and add the new data at the end of the file.

19.png

SETS

Sets are another data type used in python as well.

  • You can simply create a set using the ‘{}‘ or the ‘set()‘ method which will create a set from a sequence.

26.png

27.png

  • We can use some special set operators with this data type. Such as and(&), or(|), difference(-).

28

29

That’s all about the core data types used in python which we talked for about three or four posts. Hope now you have a basic idea about python’s core data types and how they are used. Hope to see you soon with another interesting topic in python. Thank You!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s