Introduction To Python: 1.1 Declaring Variables
Introduction To Python: 1.1 Declaring Variables
print("Value of var1 :", var1) print("Value of var2 :", var2) print("Value of var3 :", var3) print("Value of var4 :", var4)
Value of var1 : 2
Value of var2 : 5.0
Value of var3 : True
Value of var4 : Machine Learning
type( var1 )
int
type( var2 )
float
type(var3)
bool
type(var4)
str
Bigger than 1
x = 10
y = 12
# if x is greater than y
if x > y:
print ("x > y")
# if x is lesser than y
elif x < y:
print ("x < y")
else:
print ("x = y")
x < y
# Initialize x = 5
# Assign True if x is more than 10 or assign False using ternary operator isGreater = True if x > 10 else False
isGreater
False
# Initializing the sequence of numbers starting from 1 # and ending (not including) with 6
numbers = range( 1, 6 )
numbers
range(1, 6)
range?
1
2
3
4
5
# Initialize the value of 1 i = 1
print(i)
# Increment the value of i. i = i+1
1
2
3
4
Done
1.5 Functions
def addElements( a, b ):
return a + b
result = addElements( 2, 3 )
result
result
6.8
result
'pythonworkshop'
def addElements( a, b = 4 ):
return a + b
addElements( 2 )
addElements( 2, 5 )
7
1.6 Working with Collections
1.6.1.1 List
batsmen[0]
'Rohit'
['Rohit', 'Dhawan']
'Dhoni'
all_players
['Rohit',
'Dhawan',
'Kohli',
'Rahane',
'Rayudu',
'Dhoni',
'Bumrah',
'Shami',
'Bhuvi',
'Kuldeep',
'Chahal']
'Bumrah' in bowlers
True
'Rayudu' in bowlers
False
all_players.reverse()
all_players
['Chahal',
'Kuldeep',
'Bhuvi',
'Shami',
'Bumrah',
'Dhoni',
'Rayudu',
'Rahane',
'Kohli',
'Dhawan',
'Rohit']
1.7 Tuples
odiDebut
('Kohli', 2008)
odiDebut[0]
'Kohli'
tup1[1] = 2009
('Chahal',
'Kuldeep',
'Bhuvi',
'Shami',
'Bumrah',
'Dhoni',
'Rayudu',
'Rahane',
'Kohli',
'Dhawan',
'Rohit')
1.8 Set
setOfNumbers = {6,1,1,2,4,5}
setOfNumbers
{1, 2, 4, 5, 6}
wc2011.union( wc2015 )
{'Dhawan',
'Dhoni',
'Gambhir',
'Jadeja',
'Kohli',
'Rahane',
'Raina',
'Rayudu',
'Rohit',
'Sehwag',
'Tendulkar',
'Yusuf',
'Yuvraj'}
wc2011.intersection( wc2015 )
wc2015.difference( wc2011 )
1.9 Dictionary
wcWinners = { 1975: "West Indies",
1979: "West Indies",
1983: "India",
1987: "Australia",
1991: "Pakistan",
1996: "Srilanka",
1999: "Australia",
2003: "Australia",
2007: "Australia",
2011: "India"}
wcWinners[1983]
'India'
wcWinners.values()
set(wcWinners.values())
{'Australia', 'India', 'Pakistan', 'Srilanka', 'West Indies'}
wcWinners[2015] = 'Australia'
wcWinners
{1975: 'West Indies',
1979: 'West Indies',
1983: 'India',
1987: 'Australia',
1991: 'Pakistan',
1996: 'Srilanka',
1999: 'Australia',
2003: 'Australia',
2007: 'Australia',
2011: 'India',
2015: 'Australia'}
'PYTHON'
tokens = string1.split(' ') tokens
['machine', 'learning']
1.11Functional Programming
Example 1: Map
intList = [1,2,3,4,5,6,7,8,9]
# Loop through the intList, square every item and append to result list squareLi st.
for x in intList: squareList.append( pow( x, 2 ) )
print( squareList )
def square_me( x ):
return x * x
list(squareList)
list( evenInts )
[2, 4, 6, 8]
4.0
8/9
from random import sample
[0, 4, 3]
[23, 28, 76, 72, 3, 39, 63, 74, 99, 97, 57, 6, 33, 62, 24, 71, 50, 2
7, 22, 30]
9/9