Programming in UNIX Environment : Commands – mv, rm, wc, grep

The ‘mv’ command is using for renaming. And ‘rm’ command for removing files. ‘wc’ for counting lines,words,characters. The ‘grep’ command is for searching a word. ‘-v’ for searching all words except the indicating word in a line.

mv filename newname

rm filename1 filename2

wc filename1 filename2 // count lines,words,characters.
wc -l //only counts the new lines.
wc -w //print the word count.
wc -m //print the character counts.
wc -c //printf the byte counts.

grep int filename // for searching the word int.
grep -v int filename // all except int.

Programming in UNIX Environment : Commands – kill, ps, nohup, nice

The ampersand at the end of the command says that run this command and also take another from the terminal without stopping the first. It prints the process id. For stopping a process we use ‘kill’ command followed by process id. ‘ps’ command prints what that user running. Using ‘nohup’, the command will run even if the user logged out. ‘nice’ is for using another system to run our job.

$wc a* >wc.out &

$kill 6578

$ps

$ps -ag // all process that are currently running.

$nohup command & // No hangup for this command even if user logged out.

$nice expensive-command & // if your command take a lot of processor resources.

The ‘at’ command followed by time is to run our job at whatever time we want.

$ at 2340 <file

There is a file ‘.profile’ in the user’s login directory, this file contains commands which the user want to run when the user logged in. A user can set things in it.

a=/long/directory/name

$cd $a

We can put ‘a’ as given above in our profile. So we can refer that directory by the value of shell variable.

Unix file system : Devices

/dev/rp00 and /dev/rp01 are named after the DEC RP06 disc drive attached to the system. We cannot make a link to a file in another subsystem. One reason is that the inode numbers are not unique in different file systems.
$ df
$ tty
$ mesg n
$ mesg y

The ‘df’ command tells the available space in the mounted file subsystem. The ‘tty’ command tells which terminal we are using. So we can send files and informations to that terminal and it appears in the corresponding terminal. ‘mesg n’ turn off the messages. /dev/tty is a synonym for our terminal.

If we want to run a program and its output files are not importent we can redirect it to ‘/dev/null’ causes its output to be thrown away.

Unix file system : The directory hierarchy

When the system starts the file /boot is read, it then reads /unix (it is the program for the UNIX kernal itself).

The following shows some directories which reside in root and its contents.

/bin : basic programs like who,ed reside.

/etc/rc : This is a file of shell commands which is executed after the system is bootstrapped.

/etc/group : lists the members of each group.

/lib : Contains the parts of the C compiler.

/tmp : Contains temporary (short-lived) files that are created in the execution of programs.

Unix file system : Inodes

The administrative infomation is stored in the inode. It contains information such as how long it is, where the contents of the file are stored on disc etc.
System’s internal name for a file is its i-number.

$ ls -lut
$ ls -lct
$ ls -i
$ ls -ict

‘rm’ command is for removing a file. ‘mv’ command is for moving and renaming the files.

$ rm filename1 filename2
$ mv filename1 filename2

The -ut lists the file in order of usage time. -ct lists the files in the order of inode change time. The ‘-i’ option is for listing the i-numbers of the directories contents. ‘-ict’ lists inode number in order of the modifications in the inode (most recently first).

Unix file system : Permissions

The file /etc/passwd is the password file. It contains all the login information about each user. We can discover each users id and group id by looking name in /etc/passwd.

$grep name /etc/passwd

The fields in the password file looks like ‘login-id:encrypted password:uid:group-id:miscellany:login-directory:shell’. The -l option of ls command prints the permissions.

$ls -l

We can find a string like ‘-rw-r–r–‘ . The first – means that it is an ordinary file. If it is a directory there prints a ‘d’. The next three field is the permissions of the owner followed by the next three field is the permissions of the group and the next is the others permissions. We cannot edit the passwd file . But the super user can do it.

The ‘chmod’ command changes the permissions of a file. ‘permissions’ may occupy octal numbers. The ‘+’ sign is for ‘ON’ permission modes and to OFF the ‘-‘ sign is using. The ‘-w’ option is for turn off the write permisssion for all incliding the owner. ‘+x’ allows every one to execute the following command.

$ chmod permissions filename
$ chmod -w filename
$ chmod +x command
$ chmod -w .
$ chmod 775 filename //Restores the permission.

The ‘-w .’ means removing the write permission of all files in that directory.

Programming in Python: Dictionaries

Dictionaries are a fundamental data structure in Python, providing a way to store key-value pairs. Unlike lists, which are indexed numerically, dictionaries use keys to access values. Keys can be numbers, strings, or even tuples (if they are immutable). Dictionaries are unordered collections in Python versions before 3.7; starting from Python 3.7, dictionaries maintain insertion order.

Creating a Dictionary

An empty dictionary can be created using {} or the dict() constructor:

# Creating an empty dictionary
dic = {}

# Creating a dictionary with key-value pairs
dic = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}

Accessing and Modifying Dictionary Elements

You can access values using their keys, add new key-value pairs, and delete keys:

# Accessing values
print(dic['one'])  # Output: 1

# Adding a new key-value pair
dic['six'] = 6
print(dic)  # Output: {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}

# Deleting a key-value pair
del dic['five']
print(dic)  # Output: {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'six': 6}

Checking for Key Existence

To check if a key exists in a dictionary, use the in keyword:

print('one' in dic)  # Output: True
print('seven' in dic)  # Output: False

Retrieving Keys and Values

Python provides methods to get all keys, values, or key-value pairs:

# Getting all keys
print(list(dic.keys()))  # Output: ['one', 'two', 'three', 'four', 'six']

# Getting all values
print(list(dic.values()))  # Output: [1, 2, 3, 4, 6]

# Getting key-value pairs
print(list(dic.items()))  # Output: [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('six', 6)]

Creating a Dictionary from a List of Tuples

A dictionary can be created from a list of tuples, where each tuple contains a key-value pair:

a = [('aass', 23), ('iiii', 56), ('dftj', 38)]
dic_from_list = dict(a)
print(dic_from_list)  # Output: {'aass': 23, 'iiii': 56, 'dftj': 38}

Using dict() with Keyword Arguments

You can also create a dictionary using keyword arguments:

d = dict(jhjkhk=433, jkhjkhk=3434, iuijmkm=344343)
print(d)  # Output: {'jhjkhk': 433, 'jkhjkhk': 3434, 'iuijmkm': 344343}

Dictionary Methods

Here are some useful dictionary methods:

# Removing a key and returning its value
value = dic.pop('one', 'Key not found')
print(value)  # Output: 1
print(dic)  # Output: {'two': 2, 'three': 3, 'four': 4, 'six': 6}

# Merging dictionaries
other_dic = {'seven': 7, 'eight': 8}
dic.update(other_dic)
print(dic)  # Output: {'two': 2, 'three': 3, 'four': 4, 'six': 6, 'seven': 7, 'eight': 8}

Python 3 Enhancements for Dictionaries

  • Dictionary Comprehensions: You can create dictionaries using comprehensions: squares = {x: x**2 for x in range(5)} print(squares) # Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
  • The defaultdict from collections module: Provides a default value for missing keys: from collections import defaultdict dd = defaultdict(int) # Default value of int is 0 print(dd['missing_key']) # Output: 0
  • The Counter from collections module: Used to count occurrences in an iterable: from collections import Counter counter = Counter("banana") print(counter) # Output: {'b': 1, 'a': 3, 'n': 2}

Dictionaries are one of the most powerful and versatile data structures in Python. Understanding their operations and enhancements helps in writing more efficient and readable code.

Programming in Python: Sets

Introduction to Sets

A set is a built-in data type in Python that represents an unordered collection of unique elements. Sets are useful when dealing with collections of items where duplicates are not allowed, and set operations (like union, intersection, and difference) can be performed efficiently.

Creating a Set

We can create a set from a list or any iterable using the set() function:

list1 = ['apple', 'mango', 'pineapple', 'mango', 'apple', 'orange']
fruit = set(list1)
print(fruit)

Output:

{'orange', 'mango', 'apple', 'pineapple'}

As seen above, duplicate values ('mango' and 'apple') are automatically removed.

Checking Membership

You can check if an element is present in a set using the in keyword:

print('mango' in fruit)  # True
print('grape' in fruit)  # False

Set Operations

Python sets support various operations such as union, intersection, difference, and symmetric difference.

Example: Set Operations on Strings

We can also create sets from strings, which treat each character as a unique element.

a = 'abhilash'
b = 'abhijith'

set_a = set(a)
set_b = set(b)

print("Set A:", set_a)
print("Set B:", set_b)

Output:

Set A: {'a', 'b', 'i', 'h', 'l', 's'}
Set B: {'a', 'b', 'i', 'h', 'j', 't'}

Difference (-)

Elements in set_a but not in set_b:

print(set_a - set_b)  # {'s', 'l'}

Elements in set_b but not in set_a:

print(set_b - set_a)  # {'j', 't'}

Union (|)

Combines all elements from both sets:

print(set_a | set_b)  # {'a', 'b', 'i', 'h', 'j', 'l', 's', 't'}

Intersection (&)

Common elements in both sets:

print(set_a & set_b)  # {'a', 'b', 'i', 'h'}

Symmetric Difference (^)

Elements unique to each set (not in both):

print(set_a ^ set_b)  # {'s', 'l', 'j', 't'}

Additional Set Methods

Python provides several built-in methods for working with sets:

# Adding elements
a_set = {1, 2, 3}
a_set.add(4)
print(a_set)  # {1, 2, 3, 4}

# Removing elements
a_set.remove(2)
print(a_set)  # {1, 3, 4}

# Discarding elements (won't raise an error if element is absent)
a_set.discard(5)

# Clearing a set
a_set.clear()
print(a_set)  # set()

When to Use Sets

  • Removing duplicate values from a list.
  • Checking for membership (in is faster in sets than in lists).
  • Performing mathematical set operations like union, intersection, and difference.

Conclusion

Sets in Python provide an efficient way to handle collections of unique elements and perform operations like union, intersection, and difference. By understanding these fundamental operations, you can use sets to write cleaner and more efficient Python code.

Programming in Python: Tuples and Sequences

Tuples in Python are immutable sequences, meaning their elements cannot be changed after assignment. They are typically used for grouping related data.

Creating Tuples

A tuple consists of elements enclosed in parentheses, although parentheses are optional. An empty tuple is represented as ().

# Creating tuples
empty_tuple = ()
print(empty_tuple)  # Output: ()

single_element_tuple = ('one',)  # Note the comma!
print(single_element_tuple)  # Output: ('one',)

multi_element_tuple = ('one', 'two', 'three')
print(multi_element_tuple)  # Output: ('one', 'two', 'three')

Why the Comma in a Single-Element Tuple?

If you don’t include a trailing comma, Python will not recognize it as a tuple:

not_a_tuple = ('one')
print(type(not_a_tuple))  # Output: <class 'str'>

single_element_tuple = ('one',)
print(type(single_element_tuple))  # Output: <class 'tuple'>

Tuple Packing and Unpacking

Tuple packing refers to grouping multiple values into a tuple, while unpacking extracts those values into variables.

# Packing values into a tuple
t = 1223, 5676, 'one', 'two'  # Parentheses are optional here
print(t)  # Output: (1223, 5676, 'one', 'two')

# Unpacking the tuple
a, b, c, d = t
print(a)  # Output: 1223
print(d)  # Output: 'two'

Extended Unpacking (Python 3.0+)

Python allows using * to capture multiple elements during unpacking:

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

first, *middle, last = t
print(first)   # Output: 1
print(middle)  # Output: [2, 3, 4]
print(last)    # Output: 5

Immutable Nature of Tuples

Tuples are immutable, meaning elements cannot be changed after assignment:

t = (1, 2, 3)
t[0] = 10  # TypeError: 'tuple' object does not support item assignment

However, if a tuple contains mutable elements (like lists), those elements can still be modified:

t = (1, [2, 3], 4)
t[1].append(5)  # Modifying the list inside the tuple
print(t)  # Output: (1, [2, 3, 5], 4)

When to Use Tuples?

  • When you need an immutable sequence of elements.
  • When returning multiple values from a function.
  • As dictionary keys (since lists are not hashable).
  • For performance optimization since tuples are slightly faster than lists.

Conclusion

Tuples are a fundamental data structure in Python, providing an immutable sequence type. With tuple packing and unpacking, they allow for convenient assignment and handling of multiple values. While lists are more flexible, tuples serve an important role where immutability is needed.

Programming in Python: Lists

Lists in Python are one of the most versatile and widely used data structures. Unlike other data types, lists allow for extensive operations such as modification, slicing, nesting, and more. Lists are defined using square brackets [] and can contain elements of different data types, including numbers, strings, tuples, and even other lists (nested lists).

Creating and Accessing Lists

You can create a list with a mix of different types of elements:

list1 = [1, 2, 3, 'how', 'are', 'you']
print(list1)

Output:

[1, 2, 3, 'how', 'are', 'you']

You can access individual elements using their index:

print(list1[3])

Output:

'how'

Slicing Lists

Python allows you to extract parts of a list using slicing:

print(list1[3:])  # Elements from index 3 to the end
print(list1[:2])  # Elements from start up to (but not including) index 2

Output:

['how', 'are', 'you']
[1, 2]

Modifying Lists

Lists in Python are mutable, meaning their contents can be changed:

list1[:2] = 'and'  # Assigning a string to a slice
print(list1)

Output:

['a', 'n', 'd', 3, 'how', 'are', 'you']

Here, Python treats the string as an iterable and replaces the first two elements with its characters. To correctly replace multiple elements, use a list:

list1[:2] = ['and']
print(list1)

Output:

['and', 3, 'how', 'are', 'you']

Adding Elements from Another List

You can insert elements from one list into another at a specific position:

list1 = [12, 34, 56, 's', 'y']
list2 = [22, '22']
list1[3:3] = list2  # Inserts list2 at index 3 without replacing elements
print(list1)

Output:

[12, 34, 56, 22, '22', 's', 'y']

Nested Lists

Lists can also contain other lists:

list3 = [33, list2, '33']
print(list3)

Output:

[33, [22, '22'], '33']

You can access elements inside a nested list using multiple indices:

print(list3[1][1])  # Accessing '22' from the nested list
print(list3[1][0])  # Accessing 22 from the nested list

Output:

'22'
22

Removing Elements: The del Statement

The del statement removes elements from a list using their index. It can also delete the entire list.

list4 = [10, 20, 30, 40, 50]
del list4[2]  # Removes the element at index 2
print(list4)

Output:

[10, 20, 40, 50]

To remove an entire list:

del list4

Now, list4 no longer exists.

Additional List Methods

Python provides several built-in methods to work with lists:

  • append(item): Adds an item to the end of the list.
  • insert(index, item): Inserts an item at a specific index.
  • remove(item): Removes the first occurrence of an item.
  • pop(index): Removes and returns the element at the specified index (default is last item).
  • sort(): Sorts the list in place.
  • reverse(): Reverses the order of elements in the list.

Example:

nums = [5, 2, 9, 1]
nums.append(7)
nums.sort()
print(nums)

Output:

[1, 2, 5, 7, 9]

Conclusion

Python lists offer powerful functionalities, making them essential in everyday programming. With their ability to store heterogeneous elements, support slicing, allow modifications, and provide built-in methods, lists remain one of the most useful data structures in Python.

Would you like to explore more advanced list operations, such as list comprehensions or functional programming with lists? Let us know in the comments!