Monday, 4 May 2020

Python Core Examples - Core Python Examples

E:\PyExa>python --version
Python 3.7.1


E:\PyExa>python hello.py
Helloworld


if 5 > 2:
print("True")
 
True

 
#This is a comment

#Comment 1
#Comment 2
#Comment 3

"""
Comment one
Comment Two
Comment Three"
"""

a=b=c="Raja"
print(a,b,c)

Raja Raja Raja



a,b,c="Kalai","Nila","Jil"
print(a,b,c)

Kalai Nila Jil

x="Beautiful"
y="Jasmine"
print("{} is {}".format(y,x))
print("{} is {} with {}".format("Ravi","Angry","Me"))

Jasmine is Beautiful
Ravi is Angry with Me

name1="Ravi"
action="Angry"
name2="Siva"
print("{} is {} with {}".format(name1,action,name2))

Ravi is Angry with Siva

a=50
b=30
print("a={},b={},c={}".format(a,b,a+b))

a=50,b=30,c=80


a=50 # global variable
def myFunction():
print("Global variable a is {}".format(a))

myFunction()

Global variable a is 50




a = "Global"

def exaFunction():
a="Local"
print("value if a : {} - inside".format(a))
   
exaFunction()

print("value if a : {} - outside".format(a))


value if a : Local - inside
value if a : Global - outside



x = "Global"

def myfunc():
  global x
  x = "Global got changed within function"

myfunc()

print("value of x : " + x)


value of x : Global got changed within function



a="Raja"
b=3
c=5.35
d=True
print(type(a))
print(type(b))
print(type(c))
print(type(d))


<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>


a=5
b=float(a)
c=int(b)
print(a,b,c)

5 5.0 5


x=1
y=str(x)*10
print(y)

1111111111



import random
print(random.randrange(1,100))
print(random.randrange(1,100))
print(random.randrange(1,100))
print(random.randrange(1,100))
print(random.randrange(1,100))

11
18
46
81
5


a=1
print(float(a))
b=2.3
print(int(b))
c="1"
print(float(c))
print(int(c))
a=1
print(bool(a))
a=-1
print(bool(a))
a=0
print(bool(a))

1.0
2
1.0
1
True
True
False


x='Hello'
y="Hello"
if (x==y):
print("Both are same")
   

Both are same


a="""I
Love
India
"""
print(a)

I
Love
India



quote='''
Nothing
is
Impossible
'''

print(quote)

Nothing
is
Impossible




a="My Name is Billa"
print(a[0],a[1],a[-1])

M y a



a="My Name is Billa"
print(a[0:2])
print(a[3:7])
print(a[11:16])

My
Name
Billa


a="Silverster Stallone"
print(len(a))

19



a="   Arun Kumar   "
print(a.rstrip())
print(a.rstrip()  + "...")

print(a.lstrip())
print("..."+a.lstrip()) 

print(a.strip())


   Arun Kumar
   Arun Kumar...
Arun Kumar 
...Arun Kumar 
Arun Kumar



a="*****Raja-----"
print(a.lstrip("*"))
print(a.rstrip("-"))
print(a.lstrip("*").rstrip("-"))

Raja-----
*****Raja
Raja



a="ArunKUMAr KaMARaj"
print(a.lower())
print(a.upper())
print(a.capitalize())

arunkumar kamaraj
ARUNKUMAR KAMARAJ
Arunkumar kamaraj


a="Kumbakonam Degree Coffee"
b=a.replace("Coffee","Tea")
print(a)
print(b)

Kumbakonam Degree Coffee
Kumbakonam Degree Tea


a="Kumbakonam Degree Coffee"
b=a.replace("Kumbakonam","Chettinad").replace("Degree","Ginger")
print(a)
print(b)

Kumbakonam Degree Coffee
Chettinad Ginger Coffee


a="Sherin"
b=a.replace('n','l')
print(a)
print(b)

Sherin
Sheril


a="Diploma in Electrical and Electronics Engineering"
b="Electrical"
c="Computer"
print(b in a)
print(c in a)
print(c not in a)
print("z" in a)
print("z" not in a)

True
False
True
False
True


s = "Bigg Boss Season #"
s1 = s + "1"
s2 = s + "2"
s3 = s + "3"
print(s1, s2, s3)

Bigg Boss Season #1 Bigg Boss Season #2 Bigg Boss Season #3


a="My"
b='name'
c="Billa"
print("{} {} is {}".format(a,b,c))

My name is Billa


a="My"
b='name'
c="Billa"
d = "{} {} is {}".format(a,b,c)
print(d)
My name is Billa


qty = 3
product = 'Onion'
price = 18.00
sentence = "Please give me {} Kgs of  {} for {} Rupees."
print(sentence.format(qty, product, price*qty))

Please give me 3 Kgs of  Onion for 54.0 Rupees.




>>> a = input("Enter age : ")
Enter age : 32
>>> a
'32'
>>> print(a)
32


List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered and unindexed. No duplicate members.
Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.


top10Cars=list(("Lexus","Toyota","Mazda","Subaru","Kia","Infiniti","Audi","BMW"))
print(top10Cars[0:3])

top10Cars=["Lexus","Toyota","Mazda","Subaru","Kia","Infiniti","Audi","BMW"]
print(top10Cars[3:5])

for car in top10Cars:
print(car)
   
['Lexus', 'Toyota', 'Mazda']
['Subaru', 'Kia']
Lexus
Toyota
Mazda
Subaru
Kia
Infiniti
Audi
BMW

top10Cars=list(("Lexus","Toyota","Mazda","Subaru","Kia","Infiniti","Audi","BMW"))

for car in top10Cars:
if ('i' in car):
print(car)

Kia
Infiniti
Audi

print(top10Cars[-1])

BMW

print(top10Cars[:4]
['Lexus', 'Toyota', 'Mazda', 'Subaru']


top10Cars=list(("Lexus","Toyota","Mazda","Subaru","Kia","Infiniti","Audi","BMW"))
top10Cars[0]="FirstCar" #changing the value

print(top10Cars)
['FirstCar', 'Toyota', 'Mazda', 'Subaru', 'Kia', 'Infiniti', 'Audi', 'BMW']


top10Cars=list(("Lexus","Toyota","Mazda","Subaru","Kia","Infiniti","Audi","BMW"))


for car in top10Cars:
print("Length : {}, Car : {}".format(len(car),car))

Length : 5, Car : Lexus
Length : 6, Car : Toyota
Length : 5, Car : Mazda
Length : 6, Car : Subaru
Length : 3, Car : Kia
Length : 8, Car : Infiniti
Length : 4, Car : Audi
Length : 3, Car : BMW


top3Cars=list(("Lexus","Toyota","Mazda"))
top5Cars=["Subaru","Kia","Infiniti","Audi","BMW"]

topCars = top3Cars + top7Cars

print(topCars)
['Lexus', 'Toyota', 'Mazda', 'Subaru', 'Kia', 'Infiniti', 'Audi', 'BMW']


name=list(("Ram","Sankar","Narayanan"))
name.append("Vijay")
name.append("Balaji")
print(name)

['Ram', 'Sankar', 'Narayanan', 'Vijay', 'Balaji']


name=list(("Ram","Sankar","Narayanan"))
name.insert(0,"Balaji")
name.insert(1,"Vijay")
print(name)

['Balaji', 'Vijay', 'Ram', 'Sankar', 'Narayanan']

name=list(("Ram","Sankar","Narayanan"))
name.remove("Ram")
print(name)

['Sankar', 'Narayanan']


name=list(("Ram","Sankar","Narayanan"))
name.pop()
print(name)

['Ram', 'Sankar']



name=list(("Ram","Sankar","Narayanan"))
del name[1]
print(name)

['Ram', 'Narayanan']


name=list(("Ram","Sankar","Narayanan"))
del name
print(name) #NameError: name 'name' is not defined



name=list(("Ram","Sankar","Narayanan"))
name.clear()
print(name)


[]


fruits1 = ["Apple","Orange"]
fruits2 = list( ("Mango","Kiwi"))

leftt = fruits1.copy()
print(leftt)


rightt = list(fruits2)
print(rightt)

['Apple', 'Orange']
['Mango', 'Kiwi']



fruits1 = ["Apple","Orange"]
fruits2 = list( ("Mango","Kiwi"))

firstBasket = fruits1 + fruits2
print(firstBasket)


['Apple', 'Orange', 'Mango', 'Kiwi']



Capitalize the list elements:

names = ['  ARUn', 'KAlaI  ','  SIvA ',' RaJa ']
newnames = []
for v in names:
l = v.strip().lower()
fc = l[0].upper()
rc = l[1:]
newnames.append(fc+rc)

print ("Names : {}".format(names))
print ("Modified : {}".format(newnames))


C:\pyEx>python 1stUpper.py
Names : ['  ARUn', 'KAlaI  ', '  SIvA ', ' RaJa ']
Modified : ['Arun', 'Kalai', 'Siva', 'Raja']


def FirstUpper(s):
l = s.strip().lower()
fc = l[0].upper()
rc = l[1:]
return fc + rc
 

>>> print(FirstUpper(' i LOve IndIA '))
I love india

No comments:

Post a Comment

Flume - Simple Demo

// create a folder in hdfs : $ hdfs dfs -mkdir /user/flumeExa // Create a shell script which generates : Hadoop in real world <n>...