<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tushar Srivastava</title>
    <description>The latest articles on Forem by Tushar Srivastava (@tusharsrivastava).</description>
    <link>https://forem.com/tusharsrivastava</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F752899%2Fd32bcc23-1918-4878-ae23-64a60683e3b6.jpg</url>
      <title>Forem: Tushar Srivastava</title>
      <link>https://forem.com/tusharsrivastava</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tusharsrivastava"/>
    <language>en</language>
    <item>
      <title>Handling text files in python - an easy guide for beginners</title>
      <dc:creator>Tushar Srivastava</dc:creator>
      <pubDate>Sat, 20 Nov 2021 15:50:48 +0000</pubDate>
      <link>https://forem.com/tusharsrivastava/handling-text-files-in-python-an-easy-guide-for-beginners-4egd</link>
      <guid>https://forem.com/tusharsrivastava/handling-text-files-in-python-an-easy-guide-for-beginners-4egd</guid>
      <description>&lt;p&gt;When working on a large-scale web application or a project which involves working with a large amount of data, it is not logical to store all the data in variables as they are volatile in nature. We need something much more reliable and structured. This is when data files come into play. They provide an easier way to access and manipulate data.&lt;/p&gt;

&lt;p&gt;In Python, there are two types of data files:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Text files&lt;/li&gt;
&lt;li&gt;Binary files&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Text files&lt;/strong&gt; are regular data files that we all are familiar with. We can open these files in a text editor and read the content inside.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Binary files,&lt;/strong&gt; on the other hand, encode data in a specific format that can only be understood by a computer or a machine. Most of the files on our computers are stored in binary format.&lt;/p&gt;

&lt;p&gt;In this article, I will cover all the basic syntaxes for opening and closing files, and various other syntaxes Python provides to efficiently handle text files.&lt;/p&gt;

&lt;h1&gt;
  
  
  Opening a file
&lt;/h1&gt;

&lt;p&gt;The most commonly used command while handling data files in Python is &lt;code&gt;open()&lt;/code&gt;. It is used to open a file in one of the following modes-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;r (read mode)&lt;/strong&gt; - to read the contents of a file&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;w (write mode)&lt;/strong&gt; - to write to a file. Note that this mode overwrites the previously stored data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;a (append mode)&lt;/strong&gt; - to append to an existing file. This mode writes data at the end of the file and no previously stored data is lost.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;x (create mode)&lt;/strong&gt; - to create a new file. This mode returns an error if the file already exists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;r+ / +r (read and write mode)&lt;/strong&gt; - to both read and write data to the same file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;a+ / +a (read and append mode)&lt;/strong&gt; - to both read and append data to the same file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Syntax for opening a file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't specify a mode, Python opens the file in 'r' mode as default.&lt;/p&gt;

&lt;p&gt;So, &lt;code&gt;f = open("file.txt")&lt;/code&gt; is same as &lt;code&gt;f = open("file.txt", 'r')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, 'f' is the file object that contains the contents of the file opened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Opening files using 'with' clause
&lt;/h2&gt;

&lt;p&gt;Another way of opening files in Python is by using the 'with' clause, which is often considered to be the more efficient way for opening files. &lt;/p&gt;

&lt;p&gt;One advantage of using 'with' clause is that any opened file is closed automatically, in case you forget to close it manually.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;fileObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  File Object Attributes
&lt;/h2&gt;

&lt;p&gt;There are some file object attributes in Python that are used to access some more information about the opened file -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;file.closed&amp;gt;&lt;/code&gt; - returns &lt;code&gt;True&lt;/code&gt; if the file is closed and &lt;code&gt;False&lt;/code&gt; otherwise&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;file.name&amp;gt;&lt;/code&gt; - returns the name of the opened file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;file.mode&amp;gt;&lt;/code&gt; - returns the mode in which the file was opened&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Reading a file
&lt;/h1&gt;

&lt;p&gt;To read a file in Python, we first need to open the file in r, r+, or a+ mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# more code goes here...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are three ways to read the contents of a file -&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The read() method
&lt;/h2&gt;

&lt;p&gt;This method is used to read a specific number of bytes of data from the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# 'n' is the no of bytes of data
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If 'n' is not specified in the syntax or a negative number is specified, it reads the entire content of the file.&lt;/p&gt;

&lt;p&gt;Let's understand this method with an example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# reading 8 characters from the file
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'Hello wo'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# reading all the content from the file
with open("file.txt", 'r') as myFile:
    myFile.read()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'Hello world! This is content of the file'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. The readline() method
&lt;/h2&gt;

&lt;p&gt;This method is used to read a single line from the file or a specified number of bytes of data from the first line, but maximum up to the whole line.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Each line ends with a newline character '\n', which is counted as a single character&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# 'n' is the no of bytes of data
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If 'n' is not specified in the syntax or a negative number is specified, it reads the entire first line from the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# reading 10 characters from the first line
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="s"&gt;'Hello worl'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# reading the entire first line
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="s"&gt;'Hello world! This is the first line of the file'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. The readlines() method
&lt;/h2&gt;

&lt;p&gt;This method reads and returns all the lines from a text file, as members of a list. It takes no argument.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readlines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readlines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['Hello world!\n', 'Hello world!\n', 'Hello world!\n', 'Hello world!\n']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, each line in the file is returned as a member of the list with a newline character '\n' at the end. &lt;/p&gt;

&lt;p&gt;If we want to return each line as a separate list, we can use the &lt;code&gt;splitlines()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readlines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;line_split&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;splitlines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line_split&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['Hello World!']
['Hello World!']
['Hello World!']
['Hello World!']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Creating a file
&lt;/h1&gt;

&lt;p&gt;To create a file in Python, we use the &lt;code&gt;open()&lt;/code&gt; method and pass the name and mode for the file as arguments. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a file is opened in write(w) mode, an empty file is created. If a file with the same name already exists in the system, all the previous data is erased and a new empty file is created.&lt;/p&gt;

&lt;p&gt;When opened in append(a) mode, the previous data of the file remains and the new data is written at the end. However, if the file does not exist already, an empty file is created.&lt;/p&gt;

&lt;p&gt;Create(x) mode creates a new file with the specified name, but it cannot be read or edited. If a file with the same name already exists, it returns an error.&lt;/p&gt;

&lt;h1&gt;
  
  
  Writing to a file
&lt;/h1&gt;

&lt;p&gt;For writing to a file, we need to open the file in either 'write' or 'append' mode.&lt;/p&gt;

&lt;p&gt;Let's understand the difference between the two -&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Write(w)&lt;/strong&gt; mode opens the file or creates the files if it doesn't exist already, and sets the offset at the beginning of the file, meaning that the data written to this file after opening will overwrite the pre-existing data in the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Append(a)&lt;/strong&gt; mode, on the other hand, sets the offset of the file at its end after opening, which means that the new data is written to the file after the previous data, instead of overwriting it.&lt;/p&gt;

&lt;p&gt;After opening the file in either of these modes, there are two methods for writing data to the file -&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The write() method
&lt;/h2&gt;

&lt;p&gt;This method takes a string as an argument and returns the number of bytes written onto the file.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Numerical values need to be converted into strings before passing as the argument&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is some data"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'w'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. The writelines() method
&lt;/h2&gt;

&lt;p&gt;This method is used to write multiple lines to a file at the same time. It takes an iterable object like a tuple or a list, containing multiple lines, as the argument.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;writelines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at this example for a better understanding -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'w'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"line1&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"line2&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"line3&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;writelines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember to put the newline character(\n) at the end of each line.&lt;/p&gt;

&lt;p&gt;After running this code, the file will look like this -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WzoSK4uv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1637164460692/Vb_OZ1QKW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WzoSK4uv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1637164460692/Vb_OZ1QKW.png" alt="image.png" width="290" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting offsets in a file
&lt;/h1&gt;

&lt;p&gt;When we discussed the differences between 'write' and 'append' modes earlier, I mentioned offsets being set at the beginning or end of a text file.&lt;/p&gt;

&lt;p&gt;Put simply, the offset is the position of the cursor from where the data is to be read or written in the file.&lt;/p&gt;

&lt;p&gt;All the functions I talked about till now read the file data sequentially from the beginning. If we want to manipulate data in a random manner, Python gives us two functions - &lt;code&gt;seek()&lt;/code&gt; and &lt;code&gt;tell()&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  tell() function
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;tell()&lt;/code&gt; function returns the current position of the cursor or file handle of the file as an integer. This function takes no argument. When a file is opened in any mode other than 'append' mode, the initial value of &lt;code&gt;tell()&lt;/code&gt; function is zero.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tell&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  seek() function
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;seek()&lt;/code&gt; function allows us to position the file handle at a specific point in the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function takes two arguments -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;offset&lt;/strong&gt; defines the number of bytes/positions to move forward in the file&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ref&lt;/strong&gt; defines the point of reference&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Let's understand these two functions with an example -&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;First, we create a file and write some data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Creating and writing data to a file
&lt;/span&gt;&lt;span class="n"&gt;myFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'w'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world!, this data is being written onto the file."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the file, we open it again in 'read' mode and display the position of the file handle before and after reading the file. The offset is set to zero by default.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# reading the file and displaying the offset position before and after reading
&lt;/span&gt;&lt;span class="n"&gt;myFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"default position of the cursor:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tell&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tell&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"current position of the cursor:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---NTRYOos--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1637164763252/DK3Gw3qAc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---NTRYOos--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1637164763252/DK3Gw3qAc.png" alt="image.png" width="463" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see, after reading 55 characters from the file, the offset is now set to the 55th position (technically 56th position, as it starts from 0, not 1).&lt;/p&gt;

&lt;p&gt;Now, to set the offset at a specific position within the file, we use the seek() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# positioning the offset at the 10th position
&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"new position of the cursor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y1aUuJGA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1637164796967/8OhMUkhWj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y1aUuJGA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1637164796967/8OhMUkhWj.png" alt="image.png" width="467" height="47"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Closing a file
&lt;/h1&gt;

&lt;p&gt;After all the read/write operations are done, it is a good practice to close the file. Sometimes the written data is stored in cached memory and isn't actually written on the file until it is closed. Closing a file makes sure that all the unwritten data is flushed(written) on the file before closing.&lt;/p&gt;

&lt;p&gt;The syntax for closing a file in python is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fileObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that when we re-assign a file object to another file, then the previous file is automatically closed.&lt;/p&gt;

&lt;p&gt;Also, we discussed earlier that opening a file using the 'with' clause also closes the file automatically and we don't need to close it manually.&lt;/p&gt;

&lt;h1&gt;
  
  
  Deleting a file
&lt;/h1&gt;

&lt;p&gt;In order to delete a file from the system, we need to import the 'os' python module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This library has a lot of useful functions, but the one we need here is &lt;code&gt;os.remove(filename)&lt;/code&gt; We pass the name of the file as an argument. If the file does not exist, this function returns an error.&lt;/p&gt;

&lt;p&gt;A better way to delete a file in python is to check whether the file we want to delete exists. We do this by using &lt;code&gt;os.path.exist(filename)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And the code looks like this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Deleting a file
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This file does not exist!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Now that we have covered all the basic concepts for handling text files, it is time for you to practice them yourself and play around with these syntaxes. It might feel a bit overwhelming at first, but it only gets easier with practice and some experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are a few other resources you can check out -&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/python/python_file_handling.asp"&gt;W3schools - Python file handling tutorials&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://blog.micorix.com/3-python-tricks-for-reading-files"&gt;3 Python tricks for reading files&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to add more to this article to make it more informative, feel free to share them.&lt;/p&gt;

&lt;p&gt;For any queries, you can connect with me on Twitter  &lt;a href="https://twitter.com/TusharS_23"&gt;@TusharS_23&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Hope you found this article helpful. See you in the next one!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>5 Fundamental UI Design Principles For Beginners</title>
      <dc:creator>Tushar Srivastava</dc:creator>
      <pubDate>Mon, 15 Nov 2021 11:58:52 +0000</pubDate>
      <link>https://forem.com/tusharsrivastava/5-fundamental-ui-design-principles-for-beginners-4hei</link>
      <guid>https://forem.com/tusharsrivastava/5-fundamental-ui-design-principles-for-beginners-4hei</guid>
      <description>&lt;p&gt;&lt;strong&gt;User Interface Design&lt;/strong&gt; allows us to communicate and interact with a digital product, whether it be clicking on a button to submit a form, or scrolling through a gallery of photos. &lt;/p&gt;

&lt;p&gt;A great UI Design is one that the users don't even notice, yet it creates a seamless user experience for them. A bad User Interface can create frustration and lead the users to abandon the product.&lt;/p&gt;

&lt;p&gt;Now that you know why having a user-friendly Interface Design is important. Here's how you can create one.&lt;/p&gt;

&lt;p&gt;Every designer keeps in mind a set of principles and guidelines while creating a high-functioning User Interface. &lt;br&gt;
Knowing these design principles are key to improving your designs and ensuring a successful career in the design field.&lt;/p&gt;

&lt;p&gt;Here are 5 key design principles you NEED to keep in mind the next time you sit down to design -&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Clarity
&lt;/h1&gt;

&lt;p&gt;Interfaces are meant to make things easier for the users. If the user doesn't understand how to navigate through an interface, it loses all its purpose.&lt;/p&gt;

&lt;p&gt;Keep the design clean and avoid using buttons and other distracting elements if they don't serve a purpose to their user. Provide clear CTAs and feedbacks so the user knows exactly what is happening.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Consistency
&lt;/h1&gt;

&lt;p&gt;If there's one thing that can make your design look unprofessional at a glance, it's a lack of consistency.&lt;/p&gt;

&lt;p&gt;Use of similar design elements makes your design look whole and tied together. &lt;br&gt;
Use the same type of icons for navigation and stick to a color scheme to keep your design consistent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pro-tip&lt;/strong&gt;: Use an Icon Pack for a collection of cool and consistent icons to use in your designs. Here's one I suggest - &lt;a href="http://www.evericons.com/"&gt;http://www.evericons.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u4MRRw0l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633773909786/0g1Ot3zbA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u4MRRw0l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633773909786/0g1Ot3zbA.png" alt="image.png" width="880" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Color Theory
&lt;/h1&gt;

&lt;p&gt;There's a lot more to colors than I can explain in this whole article, so maybe I'll write another article talking just about this in detail. But for now, let's just stick to the basics.&lt;/p&gt;

&lt;p&gt;There are three terms that you need to understand about colors: &lt;strong&gt;Hue,&lt;br&gt;
Saturation&lt;/strong&gt; and &lt;strong&gt;Value&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hue
&lt;/h3&gt;

&lt;p&gt;The Hue is just color in its natural state, without any variation in its lightness or darkness. For example, Red, Blue, Green, etc.&lt;/p&gt;

&lt;p&gt;Refer to this image for a better understanding👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OiLN61Ys--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633775843833/12l6-87mg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OiLN61Ys--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633775843833/12l6-87mg.png" alt="image.png" width="700" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Saturation
&lt;/h3&gt;

&lt;p&gt;Saturation refers to the intensity of a color. A lower saturation level means a dull greyish color, while a higher saturation level means a more bright and vivid color.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tDOklM6L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633776011428/Y_oqesY0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tDOklM6L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633776011428/Y_oqesY0b.png" alt="image.png" width="700" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Value
&lt;/h3&gt;

&lt;p&gt;Now let's discuss Value. The Value of a color refers to the amount of lightness or darkness in each color. A value of 0% gives us White color, while a value of 100% gives us Black.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iJHEdiuQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633776167947/d4tdbPIal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iJHEdiuQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633776167947/d4tdbPIal.png" alt="image.png" width="700" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Color Schemes
&lt;/h2&gt;

&lt;p&gt;Okay, now that you understand these terms, you need to know how to use colors in harmony to create a great looking color scheme for your designs.&lt;/p&gt;

&lt;p&gt;The image below explains different ways to combine colors with harmony&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SfnSoaDL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633776503567/hzatVQOue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SfnSoaDL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633776503567/hzatVQOue.png" alt="image.png" width="880" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The 60-30-10 Rule
&lt;/h3&gt;

&lt;p&gt;When creating color schemes as a beginner, a good rule of thumb is to pick three colors. &lt;/p&gt;

&lt;p&gt;Select one of them as the Dominant/Primary color and use it for 60% of your design.&lt;/p&gt;

&lt;p&gt;Pick the Secondary color and use it for 30% of the design.&lt;/p&gt;

&lt;p&gt;The remaining third color is your Accent color which is to be used for CTAs and elements that require immediate attention.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y_ALQcaq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633777330096/Xoer8nljF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y_ALQcaq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633777330096/Xoer8nljF.png" alt="image.png" width="880" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Typography
&lt;/h1&gt;

&lt;p&gt;Almost 90% of any website, web application or mobile application is Typography. So it's right to say that it's one of the most important elements of a user interface.&lt;/p&gt;

&lt;p&gt;Here are a few tips to use typography like a pro - &lt;/p&gt;

&lt;h3&gt;
  
  
  A. Font Pairing
&lt;/h3&gt;

&lt;p&gt;Using many typefaces makes the design look cluttered and unprofessional. When starting out, use just a single font throughout your design. But if you're confident enough, keep the number of fonts to a maximum of two. &lt;/p&gt;

&lt;p&gt;Pair different types of fonts with each other to create contrast. For example, use a serif font for the heading and a sans-serif font for the paragraph text. Make sure to choose fonts that are easier to read.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7_gDtaDF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633785299860/2rWqYyN9r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7_gDtaDF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633785299860/2rWqYyN9r.png" alt="image.png" width="880" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  B. Line-Height
&lt;/h3&gt;

&lt;p&gt;When working with typography, keep in mind to give it a little bit of room to breathe, with appropriate use of &lt;strong&gt;white space&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use more line-height for smaller text, and less line-height for larger text. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lRfssKL---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633786223987/BQAlFFn_E.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lRfssKL---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633786223987/BQAlFFn_E.png" alt="image.png" width="880" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  C. Font Weight
&lt;/h3&gt;

&lt;p&gt;Use different font weights to distinguish between different text elements. Remember that contrast is key to great UI Design. &lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;pro tip&lt;/strong&gt; is to skip a weight. For example, use Regular and Bold, Medium and Extra Bold, etc.  &lt;/p&gt;

&lt;p&gt;But keep in mind that the same color might look lighter on thinner weights and darker on thicker weights.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sM71d7n0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633786785875/m8_HUC1b4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sM71d7n0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633786785875/m8_HUC1b4.png" alt="image.png" width="880" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  5. Hierarchy
&lt;/h1&gt;

&lt;p&gt;Hierarchy is important to let the users know where to look and what is important. &lt;/p&gt;

&lt;p&gt;There are many ways you can create a hierarchy. Here are a few - &lt;/p&gt;

&lt;h3&gt;
  
  
  A. Color
&lt;/h3&gt;

&lt;p&gt;You can use color to create hierarchy in one of two ways - &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;i.&lt;/strong&gt; Use darker colors for more important elements and lighter colors for less important elements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DeG1Mtfm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633787510067/OQQ21NwZa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DeG1Mtfm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633787510067/OQQ21NwZa.png" alt="image.png" width="880" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ii.&lt;/strong&gt; Use an accent color to make something pop. It demands the most attention from the user(even if it is smaller in size).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y9TcL2pV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633787683459/8IBJrjMuI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y9TcL2pV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633787683459/8IBJrjMuI.png" alt="image.png" width="880" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  B. Typography
&lt;/h3&gt;

&lt;p&gt;This might seem obvious to many, but a lot of beginners don't give it much thought.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The bigger and thicker the font size, the more important it seems.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CU_dh_Ca--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633788170831/HZ_7CJTiC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CU_dh_Ca--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633788170831/HZ_7CJTiC.png" alt="image.png" width="880" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Hierarchy in Buttons
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D26Oxm4z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633788510564/kKGfBv8a0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D26Oxm4z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1633788510564/kKGfBv8a0.png" alt="image.png" width="880" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are a few design principles and tips(among many others) that I've learned over two years of designing. If you would like to add something to make this article more informative, do share it in the comments.&lt;/p&gt;

&lt;p&gt;Here are a few other resources you should checkout to get a deeper understanding of the topics I talked about -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/17seven/ui-design-principles-c6e5e63690f0"&gt;UI Design Principles | 17Seven&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xd.adobe.com/ideas/process/ui-design/4-golden-rules-ui-design/"&gt;The 4 Golden Rules of UI Design | Nick Babich&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://uxplanet.org/top-ui-design-principles-to-keep-in-mind-bfb3ad8790c6"&gt;Top UI Design Principles To Keep In Mind | Olga Schors&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blind.com/blog/typography-manual/"&gt;10 Typography Rules | Chris Do&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;FOR MORE TIPS AND UPDATES, FOLLOW ME ON  &lt;a href="https://twitter.com/TusharS_23"&gt;TWITTER&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Hope you found this article helpful. See you in the next one!&lt;/p&gt;

</description>
      <category>design</category>
      <category>beginners</category>
      <category>uiweekly</category>
      <category>ui</category>
    </item>
  </channel>
</rss>
