Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Python Practice Day 2

The document contains examples of Python code snippets demonstrating various Python concepts like arithmetic operators, data types, conditional statements, loops, functions, classes and modules. The code snippets contain explanations and output to demonstrate the concepts.

Uploaded by

Apoorv Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python Practice Day 2

The document contains examples of Python code snippets demonstrating various Python concepts like arithmetic operators, data types, conditional statements, loops, functions, classes and modules. The code snippets contain explanations and output to demonstrate the concepts.

Uploaded by

Apoorv Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Python practice Day 2

February 1, 2022

[1]: a=1
b=3
a==b

[1]: False

[2]: a=1
b=3
a>=b

[2]: False

[3]: a=1
b=3
a<=b

[3]: True

[4]: a+b!=a-b

[4]: True

[5]: a=32
b=34
c=2
a+c==b

[5]: True

[6]: print(0 or 1)

[7]: "roger"

[7]: 'roger'

[8]: 'Roger'

1
[8]: 'Roger'

[9]: "Roger"+" is a good boy"

[9]: 'Roger is a good boy'

[11]: name = "Roger"


name += " is a good boy"
print(name)

Roger is a good boy

[12]: print(name + str(88))

Roger is a good boy88

[14]: print("Roger is " + str(8) + " years old")

Roger is 8 years old

[15]: print("""My
name
is Roger
""")

My
name
is Roger

[16]: fam = (1,3,5.0,"aa","bb","ccdd")


print(fam)

(1, 3, 5.0, 'aa', 'bb', 'ccdd')

[17]: fam[2]="Ram"

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-e5dfb891d05b> in <module>
----> 1 fam[2]="Ram"

TypeError: 'tuple' object does not support item assignment

[28]: #Create a list by name mazenet_ey,include the elements as follows:


#1,3,4,5,6;3.5,6.7,8.6,7.1;true,false;hello,you,are,well,done;NOT,OR,AND
#INSERTION,DELETION,SLICING,SORTING By upper and lower case , itemcopy

2
mazenet_ey = [1,3,4,5,6,3.5,6.7,8.6,7.1,True,False,"hello","you","are␣
,→well","done","NOT","OR","AND"]

[32]: itemcopy=mazenet_ey[:]
print(mazenet_ey)
print(itemcopy)

[1, 3, 4, 5, 6, 3.5, 6.7, 8.6, 7.1, True, False, 'hello', 'you', 'are well',
'done', 'NOT', 'OR', 'AND']
[1, 3, 4, 5, 6, 3.5, 6.7, 8.6, 7.1, True, False, 'hello', 'you', 'are well',
'done', 'NOT', 'OR', 'AND']

[38]: print(mazenet_ey)
mazenet_ey=mazenet_ey + ["suresh"]
print(mazenet_ey)

['Roger', 'Roger', 'Roger', 'Roger', 1, 3, 4, 5, 6, 3.5, 6.7, 8.6, 7.1, True,


False, 'hello', 'you', 'are well', 'done', 'NOT', 'OR', 'AND']
['Roger', 'Roger', 'Roger', 'Roger', 1, 3, 4, 5, 6, 3.5, 6.7, 8.6, 7.1, True,
False, 'hello', 'you', 'are well', 'done', 'NOT', 'OR', 'AND', 'suresh']

[39]: mazenet_ey[6::2]

[39]: [4, 6, 6.7, 7.1, False, 'you', 'done', 'OR', 'suresh']

[43]: print(sorted(mazenet_ey,key=len))

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-43-d11ae424d080> in <module>
----> 1 print(sorted(mazenet_ey,key=len))

TypeError: object of type 'int' has no len()

[46]: _abc = 78
print(_abc)

78

[1]: !122="Name" # variable name not found

/bin/bash: 122=Name: command not found

[2]: 123 = 23 #variable name invalid


print(123)

3
File "<ipython-input-2-adf02841968c>", line 1
123 = 23
^
SyntaxError: cannot assign to literal

[3]: while = 20 #using keyword as variable name

File "<ipython-input-3-3e733835859d>", line 1


while = 20
^
SyntaxError: invalid syntax

[4]: for = "Apoorv" #using keyword as variable name

File "<ipython-input-4-b1c00904b8ca>", line 1


for = "Apoorv"
^
SyntaxError: invalid syntax

[5]: if = 35
or = "Roger" #using keyword as variable name

File "<ipython-input-5-63931273a32c>", line 1


if = 35
^
SyntaxError: invalid syntax

[6]: 1+1 ; "Mazenet"

[6]: 'Mazenet'

[7]: "Mazenet" ; 1+4

[7]: 5

[8]: 1*4*77
3+99+23

[8]: 125

4
[9]: 444%4
55+5

[9]: 60

[10]: #comment in two ways


# this is single line

[11]: """This
is
multiline"""

[11]: 'This \nis \nmultiline'

[12]: type(56)

[12]: int

[13]: name = "Apoorv" print(name) # random indentation throws error

File "<ipython-input-13-fee1dbc0bc2c>", line 1


name = "Apoorv" print(name) # random indentation throws error
^
SyntaxError: invalid syntax

[14]: a = 5 b=44 c=a+b #random indentation

File "<ipython-input-14-b025d2592027>", line 1


a = 5 b=44 c=a+b #random indentation
^
SyntaxError: invalid syntax

[18]: a=5
b=3
c=a+b
print(c)#random indentation

File "<ipython-input-18-62d14c2e57f4>", line 3


c=a+b
^
IndentationError: unexpected indent

5
[17]: a=5
b=3
c=a+b
print(c)

[19]: name="Mazenet"
isinstance(name,str)

[19]: True

[20]: name=34
isinstance(name,int)

[20]: True

[21]: name="Mazenet"
isinstance(name,int)

[21]: False

[22]: name=77.9
isinstance(name,float)

[22]: True

[23]: print(type(True))

<class 'bool'>

0.0.1 aithmetic operator

[24]: 1 + 1 #

[24]: 2

[25]: 2 - 1 #1

[25]: 1

[26]: 2 * 2 #4

[26]: 4

[27]: 4 / 2 #2

6
[27]: 2.0

[28]: 4 % 3 #1

[28]: 1

[29]: 4 ** 2 #16

[29]: 16

[30]: 4 // 2 #2

[30]: 2

[31]: 2**44

[31]: 17592186044416

[32]: "roger " + "is a good dog"

[32]: 'roger is a good dog'

[33]: "I ate " + str(9) + " apples today"

[33]: 'I ate 9 apples today'

[34]: a=1
b=3
a==b

[34]: False

[35]: a=1
b=3
a>=b

[35]: False

[36]: a=1
b=3
a<=b

[36]: True

[37]: a+b!=a-b

[37]: True

7
[38]: a=32
b=34
c=2
a+c==b

[38]: True

[39]: condition1 = True


condition2 = False

not condition1

[39]: False

[40]: condition1 or condition2

[40]: True

[41]: condition1 and condition2

[41]: False

[42]: name = "Mazenet"


name += " is my friend"
name += " and zRoger is my dog"
print(name)

Mazenet is my friend and zRoger is my dog

[43]: "I ate " + str(9) + " apples today"

[43]: 'I ate 9 apples today'

[44]: print ("That's a "+str(False)+" Statement")


print("""this
is
a multiline string""")
print("""this is also a wayn to print it""")

That's a False Statement


this
is
a multiline string
this is also a wayn to print it

[46]: name = "Today I saw 9 elephants"


name.isalpha()

8
[46]: False

[49]: name1 = "TodayIsawelephants"


name1.isalpha()

[49]: True

[50]: name = "Today I saw elephants"


name.isalpha()

[50]: False

[51]: name = "Today I saw 9 elephants"


name.isalnum()

[51]: False

[52]: name = "TodayIsaw9elephants"


name.isalnum()

[52]: True

[55]: name="55.9"
name.isdecimal()

[55]: False

[56]: name="abc"
name.upper()

[56]: 'ABC'

[57]: name="ABC"
name.lower()

[57]: 'abc'

[58]: name="apoorv"
name.title()

[58]: 'Apoorv'

0.0.2 tuple

[1]: py_tuple1 = 1,2,3,4,5,6


print (py_tuple1)

9
(1, 2, 3, 4, 5, 6)

[2]: py_tuple2 = True,False,True


print (py_tuple2)

(True, False, True)

[3]: py_tuple3 = ('Apple',"Mangoes",'grapes')


print (py_tuple3)

('Apple', 'Mangoes', 'grapes')

[4]: py_tuple4 = 1.2,2.3,3.4,4.5


print (py_tuple4)

(1.2, 2.3, 3.4, 4.5)

[17]: py_tuple1 = 1,2,3,4,5,6


py_tuple2 = True,False,True
print (py_tuple1)
py_tuple1=list(py_tuple1)
py_tuple2=list(py_tuple2)

(1, 2, 3, 4, 5, 6)
None

[19]: t1=(1,2,3,["apple","mangoes"])
print(t1)

(1, 2, 3, ['apple', 'mangoes'])

[20]: t2=(1,2,3,4,{"name":"Roger","age":32.4})
print (t2)

(1, 2, 3, 4, {'name': 'Roger', 'age': 32.4})

[22]: dic1={1:"44",(1,2,3):45,"Name":True}
print(dic1)

{1: '44', (1, 2, 3): 45, 'Name': True}

[25]: print(list(dic1))
print(dic1)
dic1.get(1)

[1, (1, 2, 3), 'Name']


{1: '44', (1, 2, 3): 45, 'Name': True}

[25]: '44'

10
[29]: print(t1)
t1=list(t1)
t1[2]=45
t1=tuple(t1)
print(t1)

(1, 2, 45, ['apple', 'mangoes'])


(1, 2, 45, ['apple', 'mangoes'])

[30]: t1[2:]

[30]: (45, ['apple', 'mangoes'])

[32]: t1[-1:-3:-1]

[32]: (['apple', 'mangoes'], 45)

[33]: t1[::-1]

[33]: (['apple', 'mangoes'], 45, 2, 1)

[36]: y=("apples","mangoes",'watermelon')
z=("cherry",)
y+=z
print(y)

('apples', 'mangoes', 'watermelon', 'cherry')

[37]: y=( , )

File "<ipython-input-37-fb8f94a30c0e>", line 1


y=( , )
^
SyntaxError: invalid syntax

[40]: y=("",)
print(y)

('',)

[47]: y=["apples","mangoes",'watermelon']
z=("cherry",)
y.sort()
print(y)
print(sorted(y,reverse=True))

11
['apples', 'mangoes', 'watermelon']
['watermelon', 'mangoes', 'apples']

[52]: y=("apples","mangoes",'watermelon',"aaa","zzz","pikaboo")
print(tuple(reversed(y)))
print(y[::-1])

('pikaboo', 'zzz', 'aaa', 'watermelon', 'mangoes', 'apples')


('pikaboo', 'zzz', 'aaa', 'watermelon', 'mangoes', 'apples')

[45]: y=([1,2,3,4],)
print(type(y))

<class 'tuple'>

[53]: t1=(1,2,3,4)
t2=("apples","Mangoes","Cherry")
t3=(True,False)
t2+t1+t3

[53]: ('apples', 'Mangoes', 'Cherry', 1, 2, 3, 4, True, False)

[55]: t2=list(t2)
t2[2]="suresh"
t2=tuple(t2)
print(t2)

('apples', 'Mangoes', 'suresh')

[60]: t1=(4,5,67,8,9,4,5,5,5,"apple",'apple',4)
t2=({2:"raji",True:55},)
t1+t2

[60]: (4, 5, 67, 8, 9, 4, 5, 5, 5, 'apple', 'apple', 4, {2: 'raji', True: 55})

[65]: t1.index(5)

[65]: 1

[66]: t1[0]

[66]: 4

[67]: t1[3]

[67]: 8

[68]: t1[2]

12
[68]: 67

[69]: t1[9]

[69]: 'apple'

[70]: t1[11]

[70]: 4

[71]: t1[5]

[71]: 4

[72]: t1[-6]

[72]: 5

[73]: t1[-4]

[73]: 5

[74]: t1[-3]

[74]: 'apple'

[75]: t1[-0]

[75]: 4

[ ]: t1[0]

13

You might also like