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

vertopal.com_python_1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

vertopal.com_python_1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Operators

Print the last two numbers with duplicate value


for i in range(1,11):
print(i)
if i-1:
print(i)
else:
print(i)

1
2
3
4
5
6
7
8
9
10
10

Print multiples of 2 from first ten numbers

for i in range(1,11):
print(i*2)

2
4
6
8
10
12
14
16
18
20

First 10 numbers with their multiplication

for i in range(1,11):
for j in range(1,11):
print(i,"x",j,"=",i*j)

1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
True
First 11 numbers with help of pow symbol

for i in range(1,11):
for j in range(1,11):
print(i,"**",j,"=",i**j)

1 ** 1 = 1
1 ** 2 = 1
1 ** 3 = 1
1 ** 4 = 1
1 ** 5 = 1
1 ** 6 = 1
1 ** 7 = 1
1 ** 8 = 1
1 ** 9 = 1
1 ** 10 = 1
2 ** 1 = 2
2 ** 2 = 4
2 ** 3 = 8
2 ** 4 = 16
2 ** 5 = 32
2 ** 6 = 64
2 ** 7 = 128
2 ** 8 = 256
2 ** 9 = 512
2 ** 10 = 1024
3 ** 1 = 3
3 ** 2 = 9
3 ** 3 = 27
3 ** 4 = 81
3 ** 5 = 243
3 ** 6 = 729
3 ** 7 = 2187
3 ** 8 = 6561
3 ** 9 = 19683
3 ** 10 = 59049
4 ** 1 = 4
4 ** 2 = 16
4 ** 3 = 64
4 ** 4 = 256
4 ** 5 = 1024
4 ** 6 = 4096
4 ** 7 = 16384
4 ** 8 = 65536
4 ** 9 = 262144
4 ** 10 = 1048576
5 ** 1 = 5
5 ** 2 = 25
5 ** 3 = 125
5 ** 4 = 625
5 ** 5 = 3125
5 ** 6 = 15625
5 ** 7 = 78125
5 ** 8 = 390625
5 ** 9 = 1953125
5 ** 10 = 9765625
6 ** 1 = 6
6 ** 2 = 36
6 ** 3 = 216
6 ** 4 = 1296
6 ** 5 = 7776
6 ** 6 = 46656
6 ** 7 = 279936
6 ** 8 = 1679616
6 ** 9 = 10077696
6 ** 10 = 60466176
7 ** 1 = 7
7 ** 2 = 49
7 ** 3 = 343
7 ** 4 = 2401
7 ** 5 = 16807
7 ** 6 = 117649
7 ** 7 = 823543
7 ** 8 = 5764801
7 ** 9 = 40353607
7 ** 10 = 282475249
8 ** 1 = 8
8 ** 2 = 64
8 ** 3 = 512
8 ** 4 = 4096
8 ** 5 = 32768
8 ** 6 = 262144
8 ** 7 = 2097152
8 ** 8 = 16777216
8 ** 9 = 134217728
8 ** 10 = 1073741824
9 ** 1 = 9
9 ** 2 = 81
9 ** 3 = 729
9 ** 4 = 6561
9 ** 5 = 59049
9 ** 6 = 531441
9 ** 7 = 4782969
9 ** 8 = 43046721
9 ** 9 = 387420489
9 ** 10 = 3486784401
10 ** 1 = 10
10 ** 2 = 100
10 ** 3 = 1000
10 ** 4 = 10000
10 ** 5 = 100000
10 ** 6 = 1000000
10 ** 7 = 10000000
10 ** 8 = 100000000
10 ** 9 = 1000000000
10 ** 10 = 10000000000

i=int(input("enter a no:"))
j=int(input("enter a no:"))
print(i,"x",j,"=",i*j)

enter a no:1
enter a no:10
1 x 10 = 10

i=int(input("enter a no:"))
j=int(input("enter a no:"))
for i in range(1,9):
print(i)
for j in range (1,9):
print(j)
print(i,"x",j,"=",i*j)

enter a no:1
enter a no:9
1
1
2
3
4
5
6
7
8
2
1
2
3
4
5
6
7
8
3
1
2
3
4
5
6
7
8
4
1
2
3
4
5
6
7
8
5
1
2
3
4
5
6
7
8
6
1
2
3
4
5
6
7
8
7
1
2
3
4
5
6
7
8
8
1
2
3
4
5
6
7
8
8 x 8 = 64
num=int(input("enter a no"))
print("multiplication no table is",num)
for j in range(1,11):
print(num,"x",j,"=",num*j)

enter a no10
multiplication no table is 10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

Control statement

a=18
b=50
c=90
if a<b:
print('he is young man')
else :
print('he is middle aged man')

he is young man

a=20
b=18
c=23
if a==b:
print('a and b are equal')
elif a<b:
print('a is less than b')
elif b>c:
print('b is greater than c')
else:
print('b is less than c')

b is less than c

for loops

names=['abhi','ram','honey']
for e in names:
print(e)
if e=='ram':
break;
print(e)

abhi
abhi
ram

names=['abhi','ram','honey']
for e in names:
print(e)
if e=='ram':
continue;
print(e)

abhi
abhi
ram
honey
honey

for i in range(2,11,2):
print(i)

2
4
6
8
10

for i in range(1,11,2):
print(i)

1
3
5
7
9

b="Amazon"
print(b[0:6])
print(b[::-1])

Amazon
nozamA

am="hello"
print(am[0:5].capitalize())

Hello
am="hello world"
b=am.center(23)
print(b)

hello world

ab="helloo"
b=ab.lstrip()
print(b)

helloo

Lists

ab=["Naga","Sagar","Krishna"]
for y in ab:
print(y)
ab.append("sita")
print(ab)

Naga
Sagar
Krishna
['Naga', 'Sagar', 'Krishna', 'sita']

ab=["Naga","Sagar","Krishna","Sita"]
for y in ab:
print(y)
ab.remove("Sita")
print(ab)

Naga
Sagar
Krishna
Sita
['Naga', 'Sagar', 'Krishna']

a=["lion","alien","balloon"]
b=[y for y in a if "i" in y]
print(b)
c=[z for z in b if "en" in z]
print(c)

['lion', 'alien']
['alien']

a=["lion","tiger","horse"]
b=[e for e in a]
print(b)
b.remove("horse")
print(b)
c=['monkey' for e in a]
print(c)

['lion', 'tiger', 'horse']


['lion', 'tiger']
['monkey', 'monkey', 'monkey']

a=["hello","world"]
b=["hi","welcome"]
c=a+b
print(c)

['hello', 'world', 'hi', 'welcome']

a=["hello","world"]
b=["hi","welcome"]
for i in a:
b.append(i)
print(b)

['hi', 'welcome', 'hello', 'world']

a=["meghana","columbia","highest"]
a[1:3]=["vijay","lokesh"]
print(a)

['meghana', 'vijay', 'lokesh']

a=["apple","mango","pineapple"]
a.insert(3,"strawberry")
print(a)

['apple', 'mango', 'pineapple', 'strawberry']

b=list(("apple","cherry","orange"))
print(b)

['apple', 'cherry', 'orange']

Tuples

movies=("khaidi","kedi","rakhi")
(tamil,telugu,hindi)=movies
print(tamil)
print(telugu)
print(hindi)

khaidi
kedi
rakhi
things=("fire","fan","water","table","window","slab")
(th1,th2,*th3)=things
print(th1)
print(th2)
print(th3)

fire
fan
['water', 'table', 'window', 'slab']

things=("fire","fan","water","table","window","slab")
(th1,*th2,th3)=things
print(th1)
print(th2)
print(th3)

fire
['fan', 'water', 'table', 'window']
slab

things=("fire","fan","water","table","window","slab")
(*th1,th2,th3)=things
print(th1)
print(th2)
print(th3)

['fire', 'fan', 'water', 'table']


window
slab

Sets

thing={"kind","kim","jack"}
print(thing)

{'jack', 'kim', 'kind'}

grp1={1,2,3,4}
grp2={"hello","world","grinder"}
grp3={1.0,2,45,3.45}
grpn=grp1.union(grp2,grp3)
print(grpn)

{1, 2, 3, 4, 3.45, 'world', 'hello', 45, 'grinder'}

fan1={23,345,556,56677}
fan2={"hello","astroid","nano"}
fan3={"lome","krishna"}
fan4={"while","killed"}
mygan=fan1|fan2|fan3|fan4
print(mygan)
{'lome', 'while', 56677, 'hello', 'krishna', 556, 'nano', 'astroid',
23, 345, 'killed'}

Dictionaries

myday={
"day":"independence day",
"date":15,
"month":"August",
"year":1947
}
print(myday)

{'day': 'independence day', 'date': 15, 'month': 'August', 'year':


1947}

myday={
"day":"independence day",
"date":15,
"month":"August",
"year":1947
}
for x,y in myday.items():
print(x,y)

day independence day


date 15
month August
year 1947

a=('name1','name2','name3')
b="abhi"
ab=dict.fromkeys(a,b)
print(ab)

{'name1': 'abhi', 'name2': 'abhi', 'name3': 'abhi'}

a=('name1','name2','name3')
ab=dict.fromkeys(a)
print(ab)

{'name1': None, 'name2': None, 'name3': None}

Functions

def function():
print("hello")
remove(hello)
print("modified text:")
modified text:

def mydef(user):
print(user+"ing")
mydef("th")
mydef("larg")
mydef("scal")

thing
larging
scaling

def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
fruits.remove("apple")
print("modified list:",fruits)

apple
banana
cherry
modified list: ['banana', 'cherry']

def func(animal):
for y in animal:
print(y)
animals=["lion","tiger","wolf"]
func(animals)
print(animals)
for z in animals:
print(z)

lion
tiger
wolf
['lion', 'tiger', 'wolf']
lion
tiger
wolf

Object Oriented Programming

class Myget:
str="hello";
ptr=Myget()
print(ptr.str)

hello
class MyMem:
tri="world";
sri=MyMem()
print(sri.tri)

world

For Loops

for i in range(1,11):
print(i)
if i>11:
print(i)
else:
print(i)

1
2
3
4
5
6
7
8
9
10
10

for i in range(1,11):
print(i*2)

2
4
6
8
10
12
14
16
18
20

for i in range(0,11):
print(i+3)

3
4
5
6
7
8
9
10
11
12
13

for i in range(1,11):
print(i*2)

2
4
6
8
10
12
14
16
18
20

for i in range(1,11):
print(i*2+1)

3
5
7
9
11
13
15
17
19
21

to print odd numbers

for i in range(1,11):
print(i*2-1)

1
3
5
7
9
11
13
15
17
19
To print a even numbers with starts with 0

for i in range(1,11):
print(i*2-2)

0
2
4
6
8
10
12
14
16
18

print(100)

100

for i in range(1,11):
print(i*3+1)

4
7
10
13
16
19
22
25
28
31

for i in range(1,11):
print(i*3-1)

2
5
8
11
14
17
20
23
26
29

for i in range(1,11):
print(i*3-2)
1
4
7
10
13
16
19
22
25
28

for i in range(1,11):
if(i%2==0):
i+=2
print(i)
print(i)

1
4
3
6
5
8
7
10
9
12
12

for i in range(1,15):
for j in range(1,10):
if(i%j==0):
print(i)

1
2
2
3
3
4
4
4
5
5
6
6
6
6
7
7
8
8
8
8
9
9
9
10
10
10
11
12
12
12
12
12
13
14
14
14

for i in range(1,11):
if(i%2==1):
print(i)
elif(i<1):
print(i)

1
3
5
7
9

for i in range(1,11):
if(i%2!=0):
print(i)

1
3
5
7
9

for i in range(1,11):
if(i%2==0):
i-=1
print(i)
1
3
5
7
9

for i in range(1,11):
if(i%2==0):
print(i)
i+=1

2
4
6
8
10

You might also like