What exactly can you do with Python?
Python’s success revolves around several advantages it provides for beginners and experts alike
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.
So let's start with how Data Handling is done in Python?
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.
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.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.
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.
How File Handling is done in Python
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 real-time computing systems, the standard input-output operations is not only enough. When we use the 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 file.
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 on 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.
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.
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 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.
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
We can see that the read() method returns a new line 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).
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.
Comments
Post a Comment