-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonAssignment-1.txt
456 lines (389 loc) · 12.4 KB
/
PythonAssignment-1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
## Assignment Part-1
Q1. Why do we call Python as a general purpose and high-level programming language?
A1.Python is called general purpose and high level programming langauge because
1-its easy to code and understand.
2-low level languages are more closer to machine codes(0,1,binary codes) while high level languages are more closer to general human communication language[English in this case].
3-Codes are easily readable to others so its a general purpose language
4-It is portable means it can run on any system including Windows,Mac,Linux.
5-Supports Development in Aritifical Intelligence.
6-Codes Written in General English are converted to Machine Language[Binary Codes]
using Interpretter and Complier hence no need of processor.
7-Because of the above stated reason Python is a general and High Level Language.
Q2. Why is Python called a dynamically typed language?
A2. Python is called Dynamically typed langauage because we dont have to define type of variable while assigning any variable.
it takes care of the type of variable.
example:
in other langauges when have to define type as
var a=10:
var b=20
but in python we can simply define variables as
a=10
b=20 etc
Q3. List some pros and cons of Python programming language?
A3.Pros
1-Easy to understand and code
2-Dynamic type language
3-it have many libraries like pandas which can be used in data analysis
4-its higly scalable/Instagram,facebook and pinterest are use the power of python
5-Extensively used in Artificial Intelligence,Machine Learning,future technologies like Internet of things[IOT] etc
6-its portable means it can run on any system[WORA-Write Once and Run Anywhere]
Cons
1-it is slow compared to other non-complied langauges and requires lot of computational power
2-comes with high memory usages and high memory consumption
3-its uses reference counting and garbage collection
4-Multithreading in python
Q4. In what all domains can we use Python?
A4.Domains where Python can be used:
1-Data analysis and Data Science
Data Collection and Cleaning
Data Exploration
Data Modelling
Data Visualization
2-Machine Learning and Artificial Intelligence
3-Deep Learning Frameworks like Caffe,Tensorflow,PyTorch,Keras
2-Automations
3-Web Scraping
Scrapy and Beautiful Soup
4-Distributed Processing
PySpark
Dask
5-Finance
Fraud Detection
risk analytics
credit scoring
6-Big Data
7-Data Engineering
8-Job Scheduling with Airflow
9-Natural Language Processing
Q5. What are variable and how can we declare them?
A5.Variable in python are memory locations to store values.
a variable in python program gives data to the computer for processing.
how to declare a variable:
a variable in python can be declared by any name or even alphabet like a,aa,abc etc can be used
Example:
lets understand the concept of datatypes in variables now:
data types can be integer,strings,arrays,float,dictionary,list,typles
name="Bhawesh"
here we have declared a variable called name and assigned value "Bhawesh" to it.
Note:name variable is of string data types and enclosed in double quotes.
now lets declare a int variable:
number=10
n=10
in the above example we have declared a interger variable called number and assigned 10 to it
alternatively we can assign the same value to the variable n
Note:
variable defining can sometimes be very tricky for developers and it has to be of some sense so that it can be easily understood by other programmers
Q6. How can we take an input from the user in Python?
A6.we can take input from user in python by defining input()
Example 1:
name=input("Enter Your Name: ")
age = input("Enter Your Age: ")
print("Hi "+name+" Your age is:"+str(age))
Example 2:
number1=input("Please enter no.1-")
number2=input("Please enter no.2-")
print ("the sum of two numbers are",(int(number1)+int(number2)))
Q7. What is the default datatype of the value that has been taken as an input using input() function?
A7.string
Q8. What is type casting?
A8.Type casting refers to the conversion of the datatype
lets consider these functions
int() refers to the interger conversion
float() refers to the float conversions
str() refers to the string or text conversion
Lets consider below examples:
a=10
b=10.2
c="1"
# lets define the below variables:
a=10
b=10.1
c="1"
print ("the type of a is",type(a))
print ("the type of b is",type(b))
print ("the type of c is",type(c))
# lets add variable b and c:
# d=b+c
# print ("the sum of b and c is:",d)
# here it will give error because we are trying to add b which is float and c which is string
# to tackle ths we need to convert c which is looking like integer to integer
# so
d=b+int(c)
print ("the sum of b and c is: ",d)
Important:
Here it was possible to add b and c because we have converted c which was string type to interger type
this was the case of typecasting
Q9. Can we take more than one input from the user using single input() function? If yes, how? If no, why?
A9.Yes we can take more than one input from the user using single input() function
Example:
Taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of teachers in class: ", x)
print("Number of students in the class: ", y)
print ()
Taking three inputs at a time
x,y,z=input("Enter three values:").split()
print ("Number of teachers in class: ",x)
print ("Number of girls in the class :",y)
print ("Number of boys in the class :",z)
Q10. What are keywords?
A10.Keywords are special words reserved in python
we cannot use them in variables
As of now in latest version of python 3.8 there are 38 keywords
Let us list few of the keywords
1-True
2-False
3-else
4-Break,in ,is,lambdaa,global,conditional statements etc
Basically all that are used in programming langauges given by python itself are called keywords
Q11. Can we use keywords as a variable? Support your answer with reason.
A11.No Keywords cannot be used as variable
Example:
Suppose we define a keyword called if as variable:
if="bhawesh"
print(if)
it will throw error saying invalid syntax
Q12. What is indentation? What's the use of indentaion in Python?
A12.Python used indentation to identify a block of code
in other langauge indentation is for readibility purpose only
lets us look this with an example:
in java script we use curlibraces {} instead on identation
Example:
if(a==10){
logger.log("a is equal to 10)
}
else{
logger.log("a is not equal to 10)
}
Now the same above code is used in python by way of indentation
if a==10:
print ("a is equal to 10")
else:
print ("a is not equal to 10")
Q13. How can we throw some output in Python?
A13.Using Print() function
Example:
a=10
b=20
d=a+b
print ("the sum of a and b is",d)
output will be:
the sum of a and b is 30
Q14. What are operators in Python?
A14.Operators in Python:
Operators in python are used to perform operation in variables and values
Example:print (10+5)
There are different types of operators in python
1-Airthmetic Operators
2-Assignment Operators
3-Comparison Operatiors
4-Logical Operators
5-Identiry Operators
6-Membership Operators
7-Bitwise Operators
1-Addition Operators
+ Addition 2+3=5
- Subtraction 3-2=1
* Multiplication 3*2=6
/ Division 3/2 =1
% Modulus 3%2=1
** Exponentiation 3**2=9
// Floor Division 5//3=2
2-Assignment Operators
=
x=2
+=
x=x+2 or x+=2
-=
x=x-2 or x-=2
*=
x=x*3 or x*=3
/=
x=x/3 or x/=3
%=
x=x%3 or x%=3
//=
x=x//3 or x//=3
**=
x=x**3 or x**=3
3-Python Comparison Operators
==
!=
>
<
>=
the result will be boolean in Comparsion Operations (boolean means Either true or false)
Example:
3==3
will give true
4<3
will give false
4-Python Logical Operators
and Return true if both statment are true
or Returen true if one of the statment is true
not Reverse the result ,returns false if the result is true
5-Python Identity Operators
is Returns True if both the variables are same object
Example x is y
is not Returns True if both variables are not the same object
Example x is not y
6-Python Membership Operators
in Returns True if a sequence with the specified value is present in the object
not in Returns True if a sequence with the specified value is not present in the object
7-Python Bit Wise Operators
these are used to compare binary numbers
& (AND) Sets each bit to 1 if both bits are 1
| (OR) Sets ech bit to 1 if one of the two bits is 1
Q15. What is difference between / and // operators?
A15.Both are arithemetic operators
/ Means Division
// Means floor division which gives only interger part of division not the decimal part
Example:
print (3/2)
Result:1.5
print (5//3)
Result:1
Q16. Write a code that gives following as an output.
```
iNeuroniNeuroniNeuroniNeuron
----
A16.
a="iNeuron"
b=a*3
print (b)
Q17. Write a code to take a number as an input from the user and check if the number is odd or even.
A17.
a=int(input("Please enter the no. to check "))
if a%2==0:
print (a," is even number")
else:
print (a,"is odd number")
Q18. What are boolean operator?
A18.The logical operators and, or and not are also referred to as boolean operators.
While and as well as or operator needs two operands,which may evaluate to true or false, not operator needs one operand evaluating to true or false.
Boolean and operator returns true if both operands return true.
Examples:
>>> a=10
>>> b=15
>>> a>40 and b>40
False
>>> a>100 and b<50
False
>>> a==0 and b==0
False
>>> a>0 and b>0
True [Because both the values are greater than 0]
Example of Boolean OR
>>> a=10
>>> b=15
>>> a>30 or b>5
True
>>> a>100 or b<50
True
>>> a==0 or b==0
False
>>> a>0 or b>0
True
Example:
The not operator returns true if its operand is a false expression and returns false if it is true.
>>> a=10
>>> a>10
False
>>> not(a>10)
True
Q19. What will the output of the following?
```
1 or 0
0 and 0
True and False and True
1 or 0 or 0
```
A19.
1 or 0 = 1
0 and 0 = 0
True and False and True=False
1 or 0 or 0=1
Q20. What are conditional statements in Python?
A20.
Python supports these Logical Operators
Equals: x == y
Not Equals: x != y
Less than: x < y
Less than or equal to: x <= y
Greater than: x > y
Greater than or equal to: x >= y
These conditions can be used in several ways, most commonly in
"if statements" and loops.
An "if statement" is written by using the if keyword.
Example
If statement:
x = 1000
y = 999
if x > y:
print("x is greater than y")
Result:True so it will print x>y
Q21. What is use of 'if', 'elif' and 'else' keywords?
A21.Thease are conditional statment as are used in programming
Example:
Following Conditions has been given and we have to print statement according to the below conditions
age above 80
Senior Citizen
Age between 60 and 80
Old Aged
Age between 30 and 60
Mid Aged
less than 30
Young
age=int(input("Enter Your Age"))
if (age>=80):
print ("Senior Citizen")
elif (age>=60):
print ("Old Aged")
elif (age >=30)
print ("Mid Aged")
else:
print ("Young:)
Q22. Write a code to take the age of person as an input
and if age >= 18 display "I can vote". If age is < 18 display "I can't vote".
A22.
age=int(input("Enter Your Age"))
if age>=18:
print ("I Can vote)
else:
print ("I can't vote)
Q23. Write a code that displays the sum of all the even numbers from the given list.
```
numbers = [12, 75, 150, 180, 145, 525, 50]
```
A23.
numbers=[12, 75, 150, 180, 145, 525, 50]
sum=0
for i in numbers:
if (i%2==0):
sum=sum+i
print ("Sum of all Even Numbers is",sum)
Q24. Write a code to take 3 numbers as an input from the user and display the greatest no as output.
no1=int(input("Enter no1: "))
no2=int(input("Enter no2: "))
no3=int(input("Enter no3: "))
list=[no1,no2,no3]
greatest=no1
for i in range(1,len(list)):
if list[i]>greatest:
greatest=list[i]
print (greatest)
Q25. Write a program to display only those numbers from a list that satisfy the following conditions
- The number must be divisible by five
- If the number is greater than 150, then skip it and move to the next number
- If the number is greater than 500, then stop the loop
```
numbers = [12, 75, 150, 180, 145, 525, 50]
```
A25.
numbers = [12, 75, 150, 180, 145, 525, 50]
for i in numbers:
# print (i)
if i>500:
break
elif (i%5==0 and i<150):
print (i)