[2*num for num in range(10)][0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Assignment 18: More on While Loop with User Input
Write a Python script to print the first N even natural numbers.
Write a Python script to print the first N even natural numbers in reverse order.
Write a Python script to print the squares of the first N natural numbers.
Write a Python script to print the cubes of the first N natural numbers.
Write a Python script to print the first 10 multiples of N.
N = int(input("Enter N: "))
i = 1
while i <= N:
print(2*i, end=" ")
i += 1
print()
i = N
while i >= 1:
print(2*i, end=" ")
i -= 1
print()
i = 1
while i <= N:
print(i**2, end=" ")
i += 1
print()
i = 1
while i <= N:
print(i**3, end=" ")
i += 1
print()
i = 1
while i <= 10:
print(N*i, end=" ")
i += 1
print()Enter N: 5
2 4 6 8 10
10 8 6 4 2
1 4 9 16 25
1 8 27 64 125
5 10 15 20 25 30 35 40 45 50
Assignment 19: For Loop
Write a Python script to print each character of a string with its corresponding Unicode.
Write a Python script to print only vowels of the given string.
Write a Python script to count occurrences of spaces in a given string.
Write a Python script to print unique digits of a given integer.
Write a Python script to count the number of digits in a given number.
s = input("Enter a string: ")
for ch in s:
print(ch, ord(ch))
s = input("Enter a string: ")
for ch in s:
if ch.lower() in "aeiou":
print(ch, end=" ")
print()
s = input("Enter a string: ")
count = 0
for ch in s:
if ch == " ":
count += 1
print("Number of spaces =", count)
n = input("Enter an integer: ")
for d in set(n):
if d.isdigit():
print(d, end=" ")
print()
n = input("Enter a number: ")
count = 0
for ch in n:
if ch.isdigit():
count += 1
print("Number of digits =", count)Enter a string: siva
s 115
i 105
v 118
a 97
Enter a string: siva
i a
Enter a string: u32449172\
Number of spaces = 0
Enter an integer: akldfaajf83575
7 3 5 8
Enter a number: diidffoiajiogf3839
Number of digits = 4
Assignment 20: For Loop and Range
Write a Python script to print the first 10 multiples of 5.
Write a Python script to print the first 10 multiples of N.
Write a Python script to print the first M multiples of N.
Write a Python script to print the first 10 multiples of N in reverse order.
Write a Python script to print the table of the user’s choice.
for condition:
\(~~\)body
Assignment 21: More on For Loop and Range
Write a Python script to print the first N even natural numbers.
Write a Python script to print the first N odd natural numbers.
Write a Python script to print the squares of the first N natural numbers.
Write a Python script to print the cubes of the first N natural numbers.
Write a Python script to display all prime numbers within a range.
Example: range start = 15 range end = 45
A number is prime if it is divisible by only 1 and itself
n then it is not divisible by 1,n-1
N = int(input("Enter N: "))
for i in range(1, N+1):
print(2*i, end=" ")
print()
for i in range(1, N+1):
print(2*i-1, end=" ")
print()
for i in range(1, N+1):
print(i**2, end=" ")
print()
for i in range(1, N+1):
print(i**3, end=" ")
print()
start = int(input("Range start: "))
end = int(input("Range end: "))
for n in range(start, end+1):
if n > 1:
prime = True
for d in range(2, int(n**0.5)+1):
if n % d == 0:
prime = False
break
if prime:
print(n, end=" ")
print()Enter N: 45
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89
1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296 1369 1444 1521 1600 1681 1764 1849 1936 2025
1 8 27 64 125 216 343 512 729 1000 1331 1728 2197 2744 3375 4096 4913 5832 6859 8000 9261 10648 12167 13824 15625 17576 19683 21952 24389 27000 29791 32768 35937 39304 42875 46656 50653 54872 59319 64000 68921 74088 79507 85184 91125
Range start: 1
Range end: 594
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593
Assignment 22: Loops
Write a Python script to calculate the sum of the first N natural numbers.
Write a Python script to calculate the sum of squares of the first N natural numbers.
Write a Python script to calculate the sum of cubes of the first N natural numbers.
Write a Python script to calculate the sum of the first N odd natural numbers.
Write a Python script to calculate the sum of the first N even natural numbers.
Assignment 24: List
Write a Python script to calculate the sum of elements of a list.
Write a Python script to calculate the average of elements of a list.
Write a Python script to create a list of squares of numbers of a given list.
Write a Python script to sort list elements in descending order.
Write a Python script to create a list from a given list by selecting only even-place elements.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_720/3419637067.py in <cell line: 0>() ----> 1 list1**2 TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_720/3039887323.py in <cell line: 0>() ----> 1 list1*list1 TypeError: can't multiply sequence by non-int of type 'list'
Sum = 150
Average = 30.0
[100, 400, 900, 1600, 2500]
[50, 40, 30, 20, 10]
[20, 40]
Assignment 25: More on List
Write a Python script to create a list of the first N even natural numbers.
Write a Python script to create a list of the first N terms of a Fibonacci series.
Write a Python script to create a list of the first N prime numbers.
Write a Python script to add two matrices, each of order 3 × 3. Store matrix elements in a list of lists.
Write a Python script to create two lists from a given list of numbers such that the first list contains only positive numbers and the second list contains only non-positive numbers.
[2, 3, 5, 7]
N = int(input("Enter N: "))
even = [2*i for i in range(1, N+1)]
print(even)
N = int(input("Enter N: "))
fib = []
a, b = 0, 1
for i in range(N):
fib.append(a)
a, b = b, a+b
print(fib)
N = int(input("Enter N: "))
primes = []
num = 2
while len(primes) < N:
prime = True
for d in range(2, int(num**0.5)+1):
if num % d == 0:
prime = False
break
if prime:
primes.append(num)
num += 1
print(primes)
A = [[1,2,3],[4,5,6],[7,8,9]]
B = [[9,8,7],[6,5,4],[3,2,1]]
C = []
for i in range(3):
row = []
for j in range(3):
row.append(A[i][j] + B[i][j])
C.append(row)
print(C)
L = [5, -2, 0, 8, -7, 3, -1]
positive = [x for x in L if x > 0]
non_positive = [x for x in L if x <= 0]
print(positive)
print(non_positive)Enter N: 10
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Enter N: 10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Enter N: 10
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
[[10, 10, 10], [10, 10, 10], [10, 10, 10]]
[5, 8, 3]
[-2, 0, -7, -1]