Python Hands on Exercises
1
Variables, Data Types and Strings
1. Write Python code to take the following input from the user. Print them along with their data types.
a. vendor_name (e.g. 'cisco')
b. os_version (e.g. 6.1)
c. number_of_interfaces (e.g. 4)
Expected output:
a. vendor_name: cisco
type: <class 'str'>
b. os_version: 6.1
type: <class 'float'>
c. number_of_interfaces: 4
type: <class 'int'>
2. Write Python code to create a variable and store the following sting in it.
‘Cisco_IOS_XE_Dublin_17.10.x’. From this extract the version part and store in another variable called version.
Print version variable.
Expected output:
Version is 17.10.x
3. Write Python code to create a string variable and put the following string in it.
' this string has both leading and trailing spaces '
Find and print the number of leading as well as trailing spaces.
Expected output:
Leading spaces: 3
Trailing spaces: 4
Lists and Tuples
1. Create 2 list variables as follows and print them
a. cisco_routers with elements as csr001, csr002
b. arista_switches with elements as ar001, ar002
Expected output:
2. Create another list variable called device_grps and make the above lists as elements of this list. (Hint: Use built-in
append method of lists)
2
Expected output:
3. Create another list variable called devices and print it which should give the following output.
4. We need to create a list of certified vendors and need to ensure that someone else should not be able to accidentally
modify that list. Currently the behavior is as follows:
We are able to modify the list and add additional vendor. Make appropriate change so that this cannot be done.
Conditionals, Loops, Functions
1. Take bill amount as input from the user and store it in a variable called bill_amount. Based on the bill amount calculate
the discount to be given as per the following rules.
a. If bill amount < 1000 no discount
b. If bill amount between 1000 and 5000, 5% discount
c. If bill amount > 5000, 10% discount
d. Enhance the above code by taking age of the user also as input and store in a variable called age.
e. If the user is above 60 an additional 5% discount should be given.
Sample input and expected output:
Input: bill_amount = 1500, age = 65
Output: “Your bill amount before discount is 1500 and after a discount of 10 % it is 1350.”
3
2. Create a list variable as shown below. Using for loop print only those elements of the list which are starting with vowels.
Sample Input: names = [‘adam’, ‘sanders’, ‘edward’, ‘jessica’]
Expected output:
adam
edward
3. Ask the user to input number. Keep adding up the numbers he is entering until he enters zero and keep asking him to
enter the next number. (Hint: In addition to taking an input number form the user, we need to keep taking the input
number from him inside the while loop.)
Sample input and expected output:
Enter the first number: 10
Current total is 10.
Enter the next number: 20
Current total is 30.
Enter the next number: 45
Current total is 75.
Enter the next number: 0
Final total is 75.
4. Revisit problem 2 given above. Take the code to check whether an element of the list is starting with vowel or not and
move it into a function. While traversing the list using for loop, call the function written above to check whether current
element is starting with vowel or not.
Sample input and output remain same as in question 2, but code changes. You have to use a function.