You are on page 1of 6

Due date: 5PM: 8 Oct 2020

Assignment 1

Student Name: Iman Abbas


Student ID: 931001164
Submission Date:

Instructions:
All the programs need to be written in Jupyter and tested to see the program works. Each
line of code should have comments to describe what it does. The working code and its
output as a screen shot should be placed in the document as answer to these questions.

Question 1 (25 points):


1. Write a Python program to create a string from the first 2 and the last 2 chars of a given a
string. If the string length is less than 2, create instead an empty string.
Sample String: 'assignment1'
Expected Result: 'ast1'
Sample String: 'a1'
Expected Result: 'a1a1'
Sample String: 'a'
Expected Result: ''
Due date: 5PM: 8 Oct 2020

2. Write a Python program to create a single string from two given strings, separated by a
space and swap the first two characters of each string. 
Sample Strings: str1= 'abc', str2 = 'xyz'
Expected Result: 'xyc abz'

# First I called to define a function named 'space_and_swap'


#It creates a new string namely 'str1_swapped' which is a combination of first two characters of
string2 [:2] and remaining characters of string1 [2:]
#It creates a new string namely 'str1_swapped' which is a combination of first two characters of
string1 and remaining characters of string2
#it then returns with both new strings combined with a space. The space is indicated with
quotations with space in between.

#The function is applied to 'abc and xyz' and the result printed

#I created a string called string1


#I created a string called string2
#two modified strings are created. first is a combination of first two characters of string2 [:2] and
remaining characters of string1 [2:]
#second is a combination of first two characters of string1 and remaining characters of string2. it
is named 'str2_swapped'
#The two new modified strings are printed
Due date: 5PM: 8 Oct 2020

Question 2 (25 points):


1. Write a Python program to accept an alphabet from user and check whether an alphabet is
a vowel or consonant. 
Expected Output:
Input a letter of the alphabet: k
k is a consonant.

Input a letter of the alphabet: a


a is a vowel.
Due date: 5PM: 8 Oct 2020

2. Write a Python program to check a triangle is equilateral, isosceles or scalene.


Note:
An equilateral triangle is a triangle in which all three sides are equal.
A scalene triangle is a triangle that has three unequal sides.
An isosceles triangle is a triangle with (at least) two equal sides.
Expected Output:
Input lengths of the triangle sides:
a: 6
b: 8
c: 12
Scalene triangle

#the instructions to input lengths of the triangle side are printed


#user input is taken for side1 which is assigned the variable a
#user input is taken for side2 which is assigned the variable b
#user input is taken for side3 which is assigned the variable c
#(all input type is set to float to allow users to input to decimal points aswell)
#condition is created: side a, b and c are compared, and if equal and true, 'Equilateral triangle' is
printed
#if condition not met another condition is that any two of the sides of the triangle must be equal,
if true 'isosceles triangle' is printed
#if condition is not true then 'scalene triangle' is printed
Due date: 5PM: 8 Oct 2020

Question 3 (25 points):


1. Write a Python program which accepts the radius of a circle from the user and compute
the area. Formula for area is Area: πr2
Sample Output:
r = 1.1
Area = 3.8013271108436504

2. Write a Python program which accepts the lengths of two sides of a right-angle tringle (a
and b) and finds the length of a hypotenuse (c).
Pythagorean theorem for right-angle triangle is c2 = a2 + b2
Sample input: a = 3, b = 4
Sample output: The length of hypotenuse (c) is = 5
Due date: 5PM: 8 Oct 2020

Question 4 (25 points):


1. Write a Python program that finds the minimum, maximum and sum of a list of integers.
Sample List: [23, 22, 44, 17, 77, 55, 1, 65, 82, 2]
Expected output:
Min of list is: 1
Max of list is: 82
Sum of the list is: 388

2. Write a Python program that finds and prints the first three items, the last three items and
the middle three items of the list using list slicing operations.
Sample list: ['Red', 'Blue', 'Green', 'Black', 'White']
First three items of list are: ['Red', 'Blue', 'Green']
Last three items of list are: ['Green', 'Black', 'White']
First three items of list are: ['Blue', 'Green', 'Black']

You might also like