Let's write a program to ask the user for an integer, and then print out whether that number is EVEN or ODD If we divide an even number by 2, what will the
Write a python script that accepts a list of integers separated by spaces Convert this into a list of integers but square each element Print this list
Since the int() function cannot accept a string that doesn't appear as an integer, an error is produced as shown in lines 12 through 14 (Although line 10 is
The print function can accept any number of positional arguments, Python integers are variable-precision, so you can do computations that would
Purpose of this article: Learn how to take input from users and systems in Python Accepts integers, floats, characters, and string input from the user
8 avr 2019 · Python uses an interpreter to convert its instructions types, hence the print function accepts integer (16) along with strings here
Python integers can have an arbitrary number of digits Lists are just one of many kinds of indexable objects in Python that can accept cross-
What we are doing is taking the int value of myAge and adding 1 to it, and then we are converting that value to a string data type, allowing us to concatenate
ii) Let L be a list, each element of which is a list of ints In Python, the assignment statement L[0][0]=3 mutates the list L
index: A 0-based integer to access an element from an array index 0 1 2 3 4 5 6 7 8 9 Accept the array of integers to reverse as a parameter
1436_6data_structure_excercise.pdf DATA STRUCTURE EXCERCISE
1. Write a python script that accepts an integer n as user input (with empty prompt)
Create a list of integers from 0 to n-1
Print this list
Solution
n=int(input())
L=list(range(n))
print(L)
2. Write a python script that accepts an integer n as user input (with empty prompt)
Store the square of all the odd numbers less tha in a list
Print this list
Example:
Input:
7
Output:
[1, 9, 25]
Solution
n=int(input()) L=[] for i in range(n): if(i%2!=0): L.append(i*i) print(L)
3. Write a python script that accepts an integer n as user input (with empty prompt)
Store the square of all the odd numbers less than n in a tuple
Print that tuple
Example:
Input:
7
Output:
(1, 9, 25)
Solution
n=int(input()) T=() for i in range(n): if(i%2!=0): T=T +(i*i,) print(T)
4. Write a python script that accepts an integer, n (>= 1) from the user.
Store the first n numbers of the Fibonnaci series in a list.
Print this list.
Example:
Input:
5
Output:
[0, 1, 1, 2, 3]
Solution
n=int(input()) a=0 b=1 c=a+b
L=[a,b,c]
i=3 while(i
5.Write a python script that accepts a list of integers separated by spaces. Convert this into a list of integers but square each element. Print this list.
Example:
Input:
1 2 3 4
Output:
[1, 4, 9, 16] Solution
a=input().split() L=[] for i in a: L.append(int(i)*int(i)) print(L) 6. Write a Python Script that takes a string from the user (with empty prompt).
Convert this string into a list of characters.
Sort this list in ascending order.
Now eliminate any repeated values in this list.
Print the list.
Example
Input
hello Output
['e', 'h', 'l', 'o'] Solution
s=list(input()) s.sort() L= []
for i in s: if i not in L: L.append(i) print(L) 7. Write a Python script which will take list of integers separated by spaces as user
input (with empty prompt). Convert this into a list of integers but square each element. Store the integer and its square in a tuple, put this into a list. Print this list.
Example
Input
1 2 3 4
Solution
n=input().split() L=[] T=() for i in n: a=int(i) z=a*a T=(a,z) L.append(T) print(L) 8. Write a Python Script which will take list of integers separated by spaces from the
user(with empty prompt). For each integer, store the string version as the key and the square of the integer value as the value in a dictionary. Print the resulting dictionary.
Example
Input
1 3 5
Output
{'1': 1, '3': 9, '5': 25} Solution
n=input().split() D={} for i in n: a=int(i) D[str(i)]=a*a print(D) 9. Write a Python Script that will take 3 character month code(Lower or Upper Case)
from the user (with empty prompt).