Python: difference between list and tuple with examples

Lists and tuples both are used to store data in a sequence in Python. Lists are enclosed in square brackets [ ], whereas tuples are enclosed in parentheses ( ).

Here is an example of creating a list in Python:

cv_list = [1, 2, 3, 4, 5]

And here is an example of creating a tuple in Python:


cv_tuple = (1, 2, 3, 4, 5)

To access elements in a list or tuple, you use indexing and slicing. The first element in a list or tuple has an index of 0.

Here is an example of indexing in a list:


print(cv_list[0]) # Output: 1

And here is an example of indexing in a tuple:


print(cv_tuple[0]) # Output: 1

You can also slice a list or tuple to access a range of elements. To slice a list or tuple, you specify the start index and end index separated by a colon :.

Here is an example of slicing a list:


print(cv_list[1:3]) # Output: [2, 3]

And here is an example of slicing a tuple:


print(cv_tuple[1:3]) # Output: (2, 3)

In summary, lists and tuples are both useful data structures in Python, but they have some fundamental differences. Lists are mutable, while tuples are immutable. Lists are used when you need to add, remove, or change elements, while tuples are used when you need to store a collection of elements that won't change. Understanding the differences between these two data structures will help you make the right choice for your program.

How to prepare for Certified Kubernetes Administrator exam

Certified Kubernetes Administrator exam preparation

Hello everyone I have recently cleared the Certified Kubernetes Administrator (CKA) exam by Cloud Native Computing Foundation (CNCF), in collaboration with The Linux Foundation.

In this blog I will share the strategy I used to prepare for the exam and the important tips you should remember while you are actually giving the exam.

My CKA exam was on Kubernetes 1.22 version which is the latest exam version as of 21-Oct-21. You can see broad level domains tested in this exam here .

Solved : How to zip directories in Linux


In this post we will see how you can use zip command in Linux to compress directories and files and unzip them. zip is also very useful if you have to further uncompress the files on windows machine as by default windows allows you to open zip files without installing any extra program.

First let's install zip. Below we will show installation instruction for both Ubuntu and Redhat linux/CentOS.

Ubuntu :

Update the package repo:

root@CloudVedas:~# sudo apt-get update

Now let's install zip and unzip packages:

sudo apt-get install zip unzip -y


RHEL/CentOS:

Update the package repo:

root@CloudVedas:~# sudo yum makecache

Install the zip and unzip packages

root@CloudVedas:~# sudo yum install zip unzip


Once the zip and unzip packages are installed we can now see their usage.

The basic syntax of the zip command is below:

zip -r <output_file>.zip  <dir1> <dir2> ... <dirn>

Let's see some examples :

We will zip three directories

root@CloudVedas:~# zip -r finaldir.zip dir1 dir2 dir3

  adding: dir1/ (stored 0%)

  adding: dir1/test11 (stored 0%)

  adding: dir1/test13 (stored 0%)

  adding: dir1/tet12 (stored 0%)

  adding: dir2/ (stored 0%)

  adding: dir2/test21 (stored 0%)

  adding: dir2/test22 (stored 0%)

  adding: dir2/test23 (stored 0%)

  adding: dir3/ (stored 0%)

  adding: dir3/test31 (stored 0%)

  adding: dir3/test32 (stored 0%)

  adding: dir3/test33 (stored 0%)

root@CloudVedas:~#

When we list the contents we can see our zipped file finaldir.zip .

root@CloudVedas:~# ls -lrth

total 4.0K

drwxr-xr-x 1 root root 4.0K Aug 29 13:54 dir1

drwxr-xr-x 1 root root 4.0K Aug 29 13:54 dir2

drwxr-xr-x 1 root root 4.0K Aug 29 13:54 dir3

-rw-r--r-- 1 root root 1.8K Aug 29 13:55 finaldir.zip

root@CloudVedas:~#


You can also zip all the directories in the present working directory using this simple bash script.

for dir in $(ls -d */); do zip script_archive.zip $dir; done 


View contents of a zip file 

If you want to see the contents of a zip file without unzipping it you can use either vi or vim .

root@CloudVedas:~# vim finaldir.zip

or

root@CloudVedas:~# vi finaldir.zip

With the above commands you can easily see the list of files and directories inside the zip.


unzip

Now let's see how you can unzip a file. Be careful when you are unzipping a file with out any extra options as it will overwrite the existing contents if same name exist. Best is to unzip the file in a different directory and then copy the required files and directories as needed in the destination folder.

root@CloudVedas:~/unziptest# unzip finaldir.zip

Archive:  finaldir.zip

   creating: dir1/

 extracting: dir1/test11

 extracting: dir1/test13

 extracting: dir1/tet12

   creating: dir2/

 extracting: dir2/test21

 extracting: dir2/test22

 extracting: dir2/test23

   creating: dir3/

 extracting: dir3/test31

 extracting: dir3/test32

 extracting: dir3/test33

root@CloudVedas:~/unziptest# ls

dir1  dir2  dir3  finaldir.zip

root@CloudVedas:~/unziptest#

If you want to see other useful options on unzip you can use below command

unzip -hh

So above we have seen how you can use zip and unzip in Linux. zip is a good utility for smaller files and directories but if you need better compression you should use tar.gz. Check out our post on most useful tar command examples if you want to know more about tar.gz and tar.bz2 or other compression methods in Linux.

If you want to transfer this file out of the Linux server check our post on how to transfer files to and from linux .

Do let us know in comments section if you have any query or suggestion about this post.

Solved : Unable to locate package error in Ubuntu

Unable to locate package ubuntu

During installation of a package in ubuntu you may get an error 
Unable to locate package . 

In this post we will show you how you can fix that error and continue with your installation. 

Below is a sample error I got while installing vagrant.  But, the solution discussed below is common for any package installation in ubuntu.


root@cloudvedas:~# apt install vagrant
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vagrant

To resolve this issue you have to update your apt repo. You can simply do this by running below command as root user or use sudo .


root@cloudvedas:~# apt update

or

user@cloudvedas:~# sudo apt update


Once the apt update is successfully completed you can try the installation once more.


root@cloudvedas:~# apt install vagrant

or

user@cloudvedas:~# sudo apt install vagrant



It should work now.

Do let us know in the comment section if you are still facing any issue after following the above steps.

Solved : How to zip and unzip files in Python



In this post we will see how to zip and unzip files in python using two modules zipfile and shutil .

You can execute below commands in Jupyter notebook or IDE like Pycharm.

Note: If you are looking for python zip() function check here.

Create a zip using zipfile

First we will use a python module called zipfile to create a zip file containing multiple files.

#import required modules
import zipfile

#Let's create a zip file first by giving it a name
new_zip = zipfile.ZipFile('zip_file.zip','w')
#Add two files which we want to zip
new_zip.write("abc.txt",compress_type=zipfile.ZIP_DEFLATED)
new_zip.write("fileloop.py",compress_type=zipfile.ZIP_DEFLATED)

#Close the zip file
new_zip.close()

So, with the above steps we have created a zip file called zip_file.zip . Now let's try to extract those files in a directory.

Unzip using zipfile 

#import required modules
import zipfile

#First open the file in read mode
unzip_files = zipfile.ZipFile('zip_file.zip','r')

#Now we will unzip and put those files in a directory called extracted_dir
unzip_files.extractall("extracted_dir")

Great! so now our files are extracted in a directory called extracted_dir .


Let's try doing the same using other python module called shutil

Create a zip using shutil

First we will zip a directory.

#import required modules
import shutil
import os

#Give name of your final zipped file. .zip extension will be added automatically.
output_file = 'zip_file_new'

# Give the name of directory which you want to zip.
# If you are in same location as the directory you can simply give it's name or
# else give full path of directory.
# Check your current directory using this command
print(os.getcwd())

#full path of directory to be zipped
zip_dir = 'E:\Software\Python\CloudVedas\extracted_dir'

#Create a zip archive
shutil.make_archive(output_file,'zip',zip_dir)

Note: If your folder name has n in it at the start e.g. new_extracted_dir in that case you will have to escape it using extra "\" since \n will be treated as new line by python so in that case file path should be mentioned as 'E:\Software\Python\CloudVedas\\new_extracted_dir'

So, above we have created a zip file called zip_file_new.zip


Unzip using shutil

Now let's unzip the file zip_file_new.zip we have created earlier .

#import required modules
import shutil

#zipped file full path
zipped_file = 'E:\Software\Python\CloudVedas\zip_file_new.zip'

#full path of directory to where the zipped files will be extracted
extracted_shutil_dir = 'E:\Software\Python\CloudVedas\extracted_dir_new'

#extract the files
shutil.unpack_archive(zipped_file,extracted_shutil_dir,'zip')

Above steps has extracted all the files from our zip and put them in a directory called extracted_dir_new .

In this post we have seen two methods of zipping and unzipping files in python. Do let us know in comments section if you have any query or feedback.

If you want to extract an archive in linux shell you can check our post on how archive or unarchive using tar command.