lang = 'Python'
remarks = 'is a high level programming language'
new = lang + remarks
print(new)
HINT: Concatenation always work with same kind of datatypes. So you may need function like str()
#math is a library in Python, which contains a number of mathematical functions
import math
str1 = "The value of pi is"
p = math.pi # put a comment on dot operator
new = str1 + p
print(new)
*
operation.¶lang = 'python'
l1 = [1,2,3]
l2 = ['a','b', 'c']
t1 = ('x','y','z')
t2 = ('m','n','o')
l3 = l1 + l2
prnt('concatenation of string: ',l3) # you may assign the result to a new variable
print('replication of string: ', lang*3) # Or you can directly print the result! It's your choice!
print('concatenation of tuple: ' t1+t2) #Is there is an error? Try to find out why!
Suppose, this semester you have 5 courses.
paper code | credit |
---|---|
PHY101 | 5 |
PHY102 | 5 |
MTM103 | 5 |
CS104 | 5 |
ENVS001 | 4 |
Write a program that takes the score input from the user, print the grade and grade points(GP). Suppose GP is calculated in 10 scale, i.e. score*0.10
Score(S) | Grade(G) | |
---|---|---|
S >= 90% | O | |
80 <= S < 90 | A+ | |
60 <= S < 80 | A | |
40 <= S < 60 | B | |
S < 40 | F |
# I'm leaving this row. You may write your code here or anywhere else you like.
Modify the above code in such a way, that if a student score below 40% it'll assign 0 GP and then use that GP to calculate SGPA using the following formula.
Formula to calculate SGPA:
SGPA for nth sem($S_{n}$) : $\frac{\sum \limits_{i=1}^{N} C_{i}*GP_i}{\sum \limits_{i=1}^{N} C_i}$
Now, print the score, grade, grade points and also calculate and print the sgpa to the corresponding semester
# I'm leaving this row. You may write your code here or anywhere else you like.
Can you run a code for multiple number of time until user hit a particular input. Here is a littile help from our side. consider the following block and try to understand what is going on.
In the below code, whenever, the user hit 0
the loop will end.
Say we want to calclate factorial of different number multiple times.
def fact(n):
factorial = 1
for i in range(1,n+1): # do you understandd why n+1?
factorial *= i # equivalent to factorial = factorial * i
return factorial
flag = '123.'
while True:
num = int(input("Enter a number to calculate the factorial: "))
print("Factorial of the number is ", fact(num))
flag = input("To continue, press an key. To stop press 0: ")
if(flag == '0'): break
print("****")
# I'm leaving this row. You may write your code here or anywhere else you like.
Consider the following code
import random
for i in range(5):
print(random.randint(1,20))
# random.randint(i,j) will return a random integer in between i and j
Now, consider to create a guessing game
num
.Output may look like:
Enter a number in between 1 and 15=5
Your guess is too low
loop no: 1
Enter a number in between 1 and 15=10
Your guess is too high
loop no: 2
Enter a number in between 1 and 15=6
Your guess is too low
loop no: 3
Enter a number in between 1 and 15=7
Congrats! You got it!
# I'm leaving this row. You may write your code here or anywhere else you like.
How you'll create a list taking an input from the user?
okay, you need to define a blank list at the first place. Like, new = []
.
Now, you can use append method to add a new data in the list. Consider the following program
# say we want to make a list of float numbers
a = []
n = int(input("Enter the number of elements: "))
for i in range(n):
elements = float(input("Enter the number: "))
a.append(elements)
# you can check the new list
print(a)
# you can sort the array easily
a.sort()
print('sorted list: ', a)
# You can remove element from the list in the following manner:
# Consider a lame example :p
new = ['rat','cat','bat','ant']
new.remove('bat')
print(new)
Automate the boring stuff with Python by A. I. Sweigart
¶Character Picture Grid
Say you have a list of lists where each value in the inner lists is a one-character string, like this:
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
You can think of grid[x][y] as being the character at the x- and y-coordinates of a “picture” drawn with text characters. The (0, 0) origin will be in the upper-left corner, the x-coordinates increase going right, and w the y-coordinates increase going down.
Copy the previous grid value, and write code that uses it to print the image.
..OO.OO..
.OOOOOOO.
.OOOOOOO.
..OOOOO..
...OOO...
....O....
Hint: You will need to use a loop in a loop in order to print grid[0][0] , then grid[1][0] , then grid[2][0] , and so on, up to grid[8][0] . This will fin- ish the first row, so then print a newline. Then your program should print grid[0][1] , then grid[1][1] , then grid[2][1] , and so on. The last thing your program will print is grid[8][5] . Also, remember to pass the end keyword argument to print() if you don’t want a newline printed automatically after each print() call.
# I'm leaving this row. You may write your code here or anywhere else you like.
You are creating a fantasy video game. The data structure to model the player’s inventory will be a dictionary where the keys are string values describing the item in the inventory and the value is an integer value detail- ing how many of that item the player has. For example, the dictionary value {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12} means the player has 1 rope, 6 torches, 42 gold coins, and so on. Write a function named displayInventory() that would take any possible
Inventory:
12 arrow
42 gold coin
1 rope
6 torch
1 dagger
Total number of items: 62
HINT: Loop through the key of the dictionary. Remember how to do that? We've discussed in the class. But we forgot to put it in the notebook. Here it is
game = {'gems':20,'life':3, 'user_name':'abc123'}
for keys in game.keys():
print('keys:', keys)
for elements in game.items():
print(elements)
for values in game.values():
print(values)
# I'm leaving this row. You may write your code here or anywhere else you like.
If you've remember, we worked with Idendity matrix in our last session. We had explicitly defined the matrix there. But this time, your job is to generate the matrix. Okay, it's not that hard.
append
an integer 1 otherwise 0.# I'm leaving this row. You may write your code here or anywhere else you like.
If you've done the previous problem using function, then this problem is just a slight modification of the previous problem. Otherwise, go back and try to do the problem 5 with the function.
Now import the math
module in your program. There is method called sqrt
in the math module. So if you say math.sqrt(2)
, it will return the square root of 2. Design a program to generate the matrix whose elements are:
$A_{mn} = \sqrt{p(p+1) - n(n+1)} \delta_{m,n+1}$
where, $\delta_{a,b} = 1 $ if $a=b$, otherwise 0. and assume that $p=1$
m = 1, 0, -1 and n = 1, 0, -1
You must realise, you have to manipulate the for loop. It is easy in Python with range
function.
# I'm leaving this row. You may write your code here or anywhere else you like.
Create a substitution cipher a.k.a Caesar Cipher.
Example: msg = 'python', k= 1 encM = 'qzuipo'