What exactly can you do with Python?

Python’s success revolves around several advantages it provides for beginners and experts alike

Image for post
Image for post

Python is both popular and widely used, as the high rankings in surveys like the Tiobe Index and the large number of GitHub projects using Python attest. Python runs on every major operating system and platform, and most minor ones too. Many major libraries and API-powered services have Python bindings or wrappers, letting Python interface freely with those services or directly use those libraries.

Let's understand different methods python use for data handling

Python numbers: Python supports integers, floating-point numbers and complex numbers. They are defined as int, float and complex classes in Python. Integers and floating points are separated by the presence or absence of a decimal point. For instance, 5 is an integer whereas 5.0 is a floating-point number whereas Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.

Image for post
Image for post

Python List: In Python programming, a list is created by placing all the items (elements) inside square brackets [], separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.

Image for post
Image for post
  • Python Tuple: A tuple in Python is similar to a list. The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas we can change the elements of a list. The tuple is created by placing all the items (elements) inside parentheses (), separated by commas. The parentheses are optional, however, it is a good practice to use them.
Image for post
Image for post
  • Python String: A string is a sequence of characters. character is simply a symbol. For example, the English language has 26 characters. Computers do not deal with characters, they deal with numbers (binary). Even though you may see characters on your screen, internally it is stored and manipulated as a combination of 0s and 1s. This conversion of character to a number is called encoding, and the reverse process is decoding. ASCII and Unicode are some of the popular encodings used.
Image for post
Image for post
  • Python Sets: A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed). However, a set itself is mutable. We can add or remove items from it. Sets can also be used to perform mathematical set operations like union, intersection, symmetric difference, etc. It can have any number of items and they may be of different types (integer, float, tuple, string etc.). But a set cannot have mutable elements like lists, sets or dictionaries as its elements.
Image for post
Image for post
  • Python Dictionary: Python dictionary is an unordered collection of items. Each item of a dictionary has a key/value pair. Creating a dictionary is as simple as placing items inside curly braces {} separated by commas. While the values can be of any data type and can repeat, keys must be of immutable type (string, number or tuple with immutable elements) and must be unique.

The usage of a file system is an important thing in the field of computer science. It is the most common method used to store required information in various formats. Usually, every programmer will use standard input-output methods to test a logical concept. But, In the real-time computing systems, the standard input-output operations is not only enough. When we use standard IO method in a program, the output will remain until the execution ends. The file system is the best solution to store the information permanently in a memory device of a computer.

Opening Files in Python: Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. We can specify the mode while opening a file. In mode, we specify whether we want to read(r), write(w)or append(a) to the file. We can also specify if we want to open the file in text mode or binary mode. The default is reading in text mode. In this mode, we get strings when reading from the file. On the other hand, the binary mode returns bytes and this is the mode to be used when dealing with non-text files like images or executable files.

Image for post
Image for post

Unlike other languages, the character “a” does not imply the number 97 until it is encoded using ASCII (or other equivalent encodings). Moreover, the default encoding is platform dependent. In windows, it is cp1252 but utf-8 in Linux. So, we must not also rely on the default encoding or else our code will behave differently in different platforms. Hence, when working with files in text mode, it is highly recommended to specify the encoding type.

Closing Files in Python: When we are done with performing operations on the file, we need to properly close the file. Closing a file will free up the resources that were tied with the file. It is done using the close() method available in Python. Python has a garbage collector to clean up unreferenced objects but we must not rely on it to close the file.

Image for post
Image for post

This method is not entirely safe. If an exception occurs when we are performing some operation with the file, the code exits without closing the file.

Image for post
Image for post

So the best way to close a file is by using the “with” statement. This ensures that the file is closed when the block inside the “with” statement is exited. We don’t need to explicitly call the close() method. It is done internally.

Writing to Files in Python: To write into a file in Python, we need to open it in write(w), append(a) or exclusive creation (x) mode. We need to be careful with the (w) mode, as it will overwrite into the file if it already exists. Due to this, all the previous data are erased. Writing a string or sequence of bytes (for binary files) is done using the write() method. This method returns the number of characters written to the file.

Image for post
Image for post

This program will create a new file named “my.txt” in the current directory if it does not exist. If it does exist, it is overwritten. We must include the newline characters ourselves to distinguish the different lines.

Reading Files in Python: To read a file in Python, we must open the file in reading(r) mode.

There are various methods available for this purpose. We can use the read(size) method to read in the size number of data. If the size parameter is not specified, it reads and returns up to the end of the file. We can read the my.txt file we wrote in the above section in the following way

Image for post
Image for post

We can see that the read() method returns a newline as ‘\n’. Once the end of the file is reached, we get an empty string on further reading. We can change our current file cursor (position) using the seek() method. Similarly, the tell() method returns our current position (in the number of bytes).

Image for post
Image for post

So in this article, we learned how to handle data and how to work with the files.

Hopefully, you learn something new from the article as well as enjoy it.

Thanks for reading this article! Leave a comment below if you have any questions.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store