-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.txt
450 lines (366 loc) · 13.2 KB
/
demo.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
* python is a interpreted , general purpose , object oriented programming
language[1989]
* Guido Van Rossum , he introduced the python. Monty pythons flying circus.
FEATURES OF PYTHON:
* Simple and easy to learn
* Freeware and opensource
* High level programming language
* Platform independent
* Portability
* Dynamic typing programming language
* Both procedure oriented and object oriented programming language
* interpreted programming language
* Extensible
* Embeded
* Extensive library
LANGUAGE FUNDAMENTALS:
1.IDENTIFIERs :
* A name in the pythonn program is called as identifier.
* It can be a class name or function or module name or variable name.
RULES :
1.Alphabates(a-z/A-Z)
2.Numbers(0-9)
3.Under score(_)
4.Not allowed to start with number
Ex: cash = 100 --> valid
cas&h = 100 ---> Invalid
123total = 100 --> Invalid
total = 100 --> valid
total = 100
TOTAL = 200
firstNameLastName() --> camelcase(java)
first_name_last_name() --> snake case(python)
FirstNameLastName() --> Pascal case
first-name-last-name --> kebab case(lisp programming language)
It should print all the keywords in the python.
import keyword
keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else',
'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise',
'return', 'try', 'while', 'with', 'yield']
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DATA TYPES:
There are 2 types, 1.Fundamental datatype 2. collection datatype
1.FUNDAMENTAL DATATYPE:
* The basic building block of programming datatypes.
* There are : int , float , string , bool , complex.
* It is also called as primitive datatype.
* Data types are ones in which we can only store one value in their variables, never more than one value of the same type.
2. COLLECTION DATATYPE:
* It is used to store the data in containers are commonly known as datastructures.
* There are : List , tuple , dictionary , bytes , bytearray , set , frozenset , range , None.
* It is also called as non-primitive datatype.
* Data types are can store the multiple datatypes in one datatype.
LIST:
* It is a ordered mutable collection of elements.
* It is denoted by [].
* Insertion order is preserved.
* Hetrogenious objects are allowed.
* Duplicate values are allowed.
* Growable in nature.
* Indexing and slicing supported.
TUPLE:
* It is a ordered immutable collection of elements.
* It is denoted by ().
* Insertion order is preserved.
* Hetrogenious objects are allowed.
* Duplicate are allowed.
* Growable nature are not allowed.
* Indexing and slicing supported.
SET:
* It is a unordered and mutable collection of unique elements.
* It is denoted by {}
* Insertion order is preserved.
* Hetrogenious objects are allowed.
* Duplicates are not allowed.
* Doesnot perform Indexing and slicing.
DICTIONARY:
* It is a ordered and mutable collection of elements.
* It is denoted by {}.
* It represents the key value pair in the dictionary.
* Insertion order is preserved.
* Hetrogenious objects are allowed.
* Duplicate are not allowed.
* Indexing and slicing are not allowed.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
* When list and tuple comparison by default datatype is "TUPLE".
* When set and dictionary comparison by default datatype is "DICTIONARY".
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
OPERATORS:
* It is used to perform the operations on the data.
* It is a symbol that performs certain operations.
* There are 5 types are there : 1.Arithmetric operator , 2.Relational operator ,
3.Assignment operator , 4.Logical operator , 5.Bitwise operator , 6..Equality operator , 7.Special operator , 8.Ternary operator
-------------------------------
1.ARITHMETIC OPERATORS:
* It is used to perform the basic mathamatics between 2 arguments.
* There are : +,-,*,/,//,%,**
* / -> when 2 arguments are perform division(/) it gives the decimal[float] value.
* // -> when 2 arguments are perform (//) it gives the integer value.
* Atleast one arguments are one float and one integer value it should return the float value.
Example:
10/2 #5.0 , 10.0/2 #5.0
10//2 #5 , 10.0//2 #5.0
Note :
* If want to use the string operator for both arguments,it should return the error ----> Eg: 'sunny'+3 #Invalid , 'sunnu' + '3' # valid
-----------------------------------
2.RELATIONAL OPERATOR:
* It is used to compare the 2 arguments.
* There are: <,<=,>,>=
* we can apply the Relational operator for 'str' type also.
Example:
'sunny'<'mahesh' ---> By comparing the both string python complier observe the starting letter of both strings and give the output.
* By comparing the 's' ASCII value and 'm' ASCII value in the both arguments. ord(s) #115 , ord(m) #109
----------------------------------------
3. Equality operator:
* It is used to compare the value of given 2 arguments.
* There are : == , !=
Example:
10=='sunny' #False
10==10.0 #True
10=='10' #False
10.10 == 10.1 # True
----------------------------------------------------
4.Logical operator:
* It is used to combine multiple conditions together and evaluate them as a single boolean expression.
* There are 3 types are there : 'and','or','not'.
* 'and' --> If both arguments is True then only result is true.
* 'or' --> If one argument is True then result is true.
* 'not' --> Complement.
Example:
True and False #False
True or False #True
not False #True
For non-boolean type behaviours:
--------------------------------------------------
-->0 means False
-->non-zero means True
-->Empty string always False.
x and y:
------------
If x evaluates to False return x otherwise return y.
Ex:
10 and 20 #20
0 and 10 #0
-->If the first argument is zero then result is zero otherwise result y.
x or y:
----------
If x evaluates to True then result is x otherwise result is y.
Ex:
10 or 20 #10
0 or 20 #20
not x:
---------
-->If x evaluates to False then the result is True otherwise False
Ex:
not 10 #False
not '' #True
Ex:
'sunny' and 'mahesh' #'mahesh'
'' and 'sunny' #''
'' or 'sunny' #'sunny'
'sunny' or '' #'sunny'
not 'radhika' #False
10 or 10/0 #10
0 or 10/0 #ZeroDivisionError
--------------------------------------------
5. Bitwise operator:
* It is used to perform Bitwise calculations on integers.
* The integers are converted into binary format and then operations are performed bit by bit.
* It works on integers only and the final output is returned in the decimal format.
------------------------------
&, |, ^, ~, <<, >>
0 & 0==>0 0 | 0==>0 0 ^ 0==>0
0 & 1==>0 0 | 1==>1 0 ^ 1==>1
1 & 0==>0 1 | 0==>1 1 ^ 0==>1
1 & 1==>1 1 | 1==>1 1 ^ 1==>0
&==>If both bits are 1 then only result is 1 otherwise 0.
|==>If atleast one bit is 1 then the result is 1 otherwise result is 0.
^==>x-or==.If bits are differentr then only result is 1 otherwise result is 0.
~==>Bitwise complement operator
Ex:
4 & 5 #4
4 | 5 #5
4 ^ 5 #1
~4 #-5
<<(left shift operator):
---------------------------------
10<<2 #40
>>(Right shift operator):
-----------------------------------
10>>2 #2
Ex:
True & False #False
True | False # True
True ^ False #True
~True #
True << 2 #4
True >> 2 #0
-----------------------------------
6.Assignment operators:
-------------------------------------
-->We can use assignment operator to assign a value to the variable.
Ex:
x = 10
-->We can cobmine assignment operator with some other operator to form compound assignment operator.
Ex:
x += 10 ===>x = x+10
-->Compound assignment operators are:
+=,-=,*=,/=,..............
-->No increment and decrement operators in python.
Ex:
x++(Invalid)
x--(Invalid)
+(+x) ==>(valid)
-(-x)==>(valid)
-----------------------------------------------
7.Ternary Operator:
------------------------------
-->In java we can use:
x = (condition)?first value:second value
Ex:
x = (10<20)?30:40
print(x)
-->In python we can use:
x = first value if condition else second value.
Ex:
x = 30 if 10<20 else 40
x #30
x = 30 if 10>20 else 40
x #40
-->first value if condition-1 else second value if condition-2 else third value
x = 10 if 20<30 else 40 if 50<60 else 70
x #10
x = 10 if 20>30 else 40 if 50<60 else 70
x #40
x = 10 if 20>30 else 40 if 50>60 else 70
x #70
-------------------------------------------
8.Special Operators:
--------------------------------
1.Identity operators:
We can use identity operators for address comparision. 2-identity operators are available.
-->is
-->is not
r1 is r2 returns True if both r1 and r2 are pointing to the same object.
r1 is not r2 returns True if both r1 and r2 are not pointing to the same object.
Ex:
a = 10
b = 10
a is b #True
a is not b #False
Ex:
l1 = ['one','two','three']
l2 = ['one','two','three']
l1 is l2 #False
l1 == l2 #True
Note:
We can use is operator for address comparision where as == operator for content comparision.
2.Membership operator:
We can use membership operators to check whether the given object present in the given collection.(It may be list,tuple,string....)
in==>Returns True if the given object present in the specified collection.
not in==>Returns True if the given object not present in the specified collection.
Ex:
l = [10,20,30]
30 in l #True
50 in l #False
50 not in l #True
30 not in l #False
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
INPUT AND OUTPUT STREAMS:
----------> INPUT:
* There are 2 functions are:
1.raw_input()
2.input()
1. Raw_input():
* This function always read the data from the keyword in the form of string format.
* we have to convert that string type to our required type by using type casting methods.
2. Input():
* It is a function can be used to read data directly in our required format.
* we are not required to perform the type casting.
Note:
* But in python-3 version we have only input() method and raw_input() method is not available.
* python-3 input() function behaviour exactly same as raw_input() method in python-2 i.e every input value is treated as str type only.
EVAL FUNCTION:
* eval() function can take a string and evaluate the result.
* x = eval(input("Enter a expression: "))
print(x)
COMMAND LINE ARGUMENTS:
* The arguments which are passing at the time of execution are called as command line arguments.
Example : D:\Coaching-Python-Full-Stack\input and Output> py.input.py
* withinn the python program this command line args are available in "argv". which is present in "sys" module.
* The type of arg is not an array it is a list.
Note:
* argv[0] represents name of the program,but not the first command line argument.
* argv[1] represents first command line argument.
* argv[2] represents second command line argument.
Example:
* from sys import argv
print(type(argv))
-------------> OUTPUT:
Output Streams:
-------------------------
We can use print() function to display output
Form-1:print() without arg
-----------------------------------------
Just it prints new line character
Form-2:print(string)
------------------------------
print('Hello World')
print('Hello\nWorld')
print('Hello\tWorld')
print(5*'Hi ')
print('Hi '*5)
print('Hello'+'World')
print('Hello','World')
Form-3:print() with variable number of args
------------------------------------------------------------------
a,b,c = 10,20,30
print('The values are:',a,b,c)
-->Bydefault output values are separated by space. If we want we can specify separator by using 'sep' attribute.
a,b,c = 10,20,30
print(a,b,c,sep=':')
print(a,b,c,sep='-')
Form-4:print() with end attribute
--------------------------------------------------
print('Hello')
print('Radhika')
print('How r u?')
-->If we want output in the same line with space.
print('Hello',end=' ')
print('Radhika',end=' ')
print('How r u?')
Form-5:print(object) statement
-----------------------------------------------
We can pass any object(like list,tuple,set.....) as arg to the print.
Ex:
l = [10,20,30]
t = (10,20,30)
print(l)
print(t)
Form-6:print(string,variable list)
-------------------------------------------------
s = 'Mahesh'
a = 50
s1 = 'Python'
s2 = 'Django'
print('Hello',s,'your age is:',a)
print('You are teaching',s1,'and',s2)
Form-7:print(formatted string)
----------------------------------------------
%d-->int
%f-->float
%s-->string type
Syn:
print('formatted string'%(variablelist))
a,b,c = 10,20,30
print('a value is %d'%a)
print('b value is %d and c value is %d'%(b,c))
Form-8:print() with replacement operator {}
------------------------------------------------------------------
name = 'Mahesh'
sal = 10000
gf = 'Sunny'
print('Hello {} your salary {} and your GF {} is waiting'.format(name,sal,gf))
print('Hello {0} your salary {1} and your GF {2} is waiting'.format(name,sal,gf))
print('Hello {n} your salary {s} and your GF {g} is waiting'.format(s=sal,g=gf,n=name))