PYTHON BASICS
PYTHON BASICSimport sys -allows us to enter command line args
#!/bin/python3
#Print string
print("Strings and stringsss:")
print('helloo world')
print("""Hello, this is a multi-
line string..""")
print("This is"+" a string") #concatenation in a print
print("\n") #new line
#Maths
print("Math time:")
print(34567+1234)
print(50-50)
print(50*50)
print(50/3)
print(50+50*50)
print(50**3) #exponents
print(50 % 3) #modulo
print(50 // 3) #number without leftovers
#Variables and Methods
print('Fun with variables:')
qoute = "All is fair in love and war"
print(qoute)
print(len(qoute)) #length
print(qoute.upper()) #uppercase
print(qoute.lower()) #lowercase
print(qoute.title()) #title (words start with caps)
name="Illuminati"
age=18 #int int(29)
weight=52.56 #float float(52.26)
print(int(weight)) #cuts of decimals
print("I am "+name+", my age is "+ str(age) +" and my weight is "+ str(weight) +"kgs")
age += 1
print(age)
print("\n")
#Functions
print("Functionssss")
def who_am_i():
name1 = "Illuminati"
age = 18;
print("I am "+name1+", my age is "+ str(age))
who_am_i()
#adding parameters
print("\nParameters")
def add_hundred(num):
print(num+100)
add_hundred(100)
#adding multiple paramters
print("\nMultiple Parameters")
def add(x,y):
print(x+y)
add(7,7)
add(23456,0)
#using return
print("\nUsing return")
def multiply(x,y):
return x*y
print(multiply(7,7))
#SquareRoot
def sqr(x):
return x**2
print(sqr(7))
#Boolean Expressions
print("\nBoolean Expressions")
bool1 = True
bool2 = 3*3 == 9
bool3 = False
bool4 = 3*3 != 9
print(bool1,bool2,bool3,bool4)
print(type(bool1))
#Relational and Boolean Operators
print("\nRelational and Boolean Operators")
greater_than = 7>5
less_than = 5<7
greater_than_equal = 7 >= 7
lesser_than_equal = 7 <= 7
print(greater_than,less_than,greater_than_equal,lesser_than_equal)
#Test AND, OR, NOT
print("\nTesting and,or,not")
test_and = (7 > 5) and (1 < 2)
test_or = (7 >5) or (2 > 1)
test_not = not True
print(test_and,test_or,test_not)
#Conditional Statements
print("\nConditional Statements")
print("If else -one parameter")
def pen(money):
if money >= 10:
return "You can buy a pen"
else:
return "Spending the money isn't smart"
print(pen(15))
print(pen(5))
print("\nIf else -two parameters")
def alcohol(age,money):
if age >= 18:
if money >= 100:
return "Here is your drink"
else:
return "You don't have enough money"
else:
return "Grow up kid!!!"
print(alcohol(18,250))
print(alcohol(17,250))
print(alcohol(34,95))
#the above if-else condition can be written like as follows
#if (age >= 18) and (money >= 100):
# return "Here's your drink"
#elif (age >= 18) and (money <= 100):
# return "Come back with more money"
#elif (age < 18) and (money >= 100):
# return "Grow Up kid!!"
#else:
# return "You are underage and poor to buy a drink"
#Lists
print("\nLists")
fruits = ["apple","orange","grapes","strawberry"]
print(fruits)
print(fruits[0]) #0,1,2,3...
print(fruits[1:])
print(fruits[:1])
print(fruits[-1])
print(len(fruits))
print("\nAdding items")
fruits.append("Papaya")
print(fruits)
print("\nRemoving items")
fruits.pop(4)
print(fruits)
print("\nCombining Lists")
fruits = ["apple","orange","grapes","strawberry"]
person = ["me","you","him","her"]
combined = zip(fruits,person)
print(list(combined))
#Tuples
print("\nTuples:a list but cannot be modified,uses ()")
grades = ("A","B","C","D","E","F")
print(grades[2])
#Loops
print("\nFor loop, full iterate")
vegetables = ["onion","spinach","cabbage"]
for x in vegetables:
print(x)
print("\nWhile loop, executes as long as true")
i = 1
while i < 10:
print(i)
i += 1
#Print string
print("Strings and stringsss:")
print('helloo world')
print("""Hello, this is a multi-
line string..""")
print("This is"+" a string") #concatenation in a print
print("\n") #new line
#Maths
print("Math time:")
print(34567+1234)
print(50-50)
print(50*50)
print(50/3)
print(50+50*50)
print(50**3) #exponents
print(50 % 3) #modulo
print(50 // 3) #number without leftovers
#Variables and Methods
print('Fun with variables:')
qoute = "All is fair in love and war"
print(qoute)
print(len(qoute)) #length
print(qoute.upper()) #uppercase
print(qoute.lower()) #lowercase
print(qoute.title()) #title (words start with caps)
name="Illuminati"
age=18 #int int(29)
weight=52.56 #float float(52.26)
print(int(weight)) #cuts of decimals
print("I am "+name+", my age is "+ str(age) +" and my weight is "+ str(weight) +"kgs")
age += 1
print(age)
print("\n")
#Functions
print("Functionssss")
def who_am_i():
name1 = "Illuminati"
age = 18;
print("I am "+name1+", my age is "+ str(age))
who_am_i()
#adding parameters
print("\nParameters")
def add_hundred(num):
print(num+100)
add_hundred(100)
#adding multiple paramters
print("\nMultiple Parameters")
def add(x,y):
print(x+y)
add(7,7)
add(23456,0)
#using return
print("\nUsing return")
def multiply(x,y):
return x*y
print(multiply(7,7))
#SquareRoot
def sqr(x):
return x**2
print(sqr(7))
#Boolean Expressions
print("\nBoolean Expressions")
bool1 = True
bool2 = 3*3 == 9
bool3 = False
bool4 = 3*3 != 9
print(bool1,bool2,bool3,bool4)
print(type(bool1))
#Relational and Boolean Operators
print("\nRelational and Boolean Operators")
greater_than = 7>5
less_than = 5<7
greater_than_equal = 7 >= 7
lesser_than_equal = 7 <= 7
print(greater_than,less_than,greater_than_equal,lesser_than_equal)
#Test AND, OR, NOT
print("\nTesting and,or,not")
test_and = (7 > 5) and (1 < 2)
test_or = (7 >5) or (2 > 1)
test_not = not True
print(test_and,test_or,test_not)
#Conditional Statements
print("\nConditional Statements")
print("If else -one parameter")
def pen(money):
if money >= 10:
return "You can buy a pen"
else:
return "Spending the money isn't smart"
print(pen(15))
print(pen(5))
print("\nIf else -two parameters")
def alcohol(age,money):
if age >= 18:
if money >= 100:
return "Here is your drink"
else:
return "You don't have enough money"
else:
return "Grow up kid!!!"
print(alcohol(18,250))
print(alcohol(17,250))
print(alcohol(34,95))
#the above if-else condition can be written like as follows
#if (age >= 18) and (money >= 100):
# return "Here's your drink"
#elif (age >= 18) and (money <= 100):
# return "Come back with more money"
#elif (age < 18) and (money >= 100):
# return "Grow Up kid!!"
#else:
# return "You are underage and poor to buy a drink"
#Lists
print("\nLists")
fruits = ["apple","orange","grapes","strawberry"]
print(fruits)
print(fruits[0]) #0,1,2,3...
print(fruits[1:])
print(fruits[:1])
print(fruits[-1])
print(len(fruits))
print("\nAdding items")
fruits.append("Papaya")
print(fruits)
print("\nRemoving items")
fruits.pop(4)
print(fruits)
print("\nCombining Lists")
fruits = ["apple","orange","grapes","strawberry"]
person = ["me","you","him","her"]
combined = zip(fruits,person)
print(list(combined))
#Tuples
print("\nTuples:a list but cannot be modified,uses ()")
grades = ("A","B","C","D","E","F")
print(grades[2])
#Loops
print("\nFor loop, full iterate")
vegetables = ["onion","spinach","cabbage"]
for x in vegetables:
print(x)
print("\nWhile loop, executes as long as true")
i = 1
while i < 10:
print(i)
i += 1
#!/bin/python3
#importing
print("IMPORTING")
import sys #system functions and parameters
from datetime import datetime
print(datetime.now())
from datetime import datetime as dt #importing with as alias
print(dt.now())
def nl():
print("\n") #using fun nl to imply new lines easily
nl();
#Advanced Strings
print("Advanced Strings")
my_name = "illuminati"
print(my_name[0]) #first initial
print(my_name[-1]) #last initial
sentence = "This is a sentence"
print(sentence[:4]) #capturing the first word
print(sentence[-8:]) #capturing the last word
splitsent = sentence.split() #splits by space
print(splitsent)
splitjoin = " ".join(splitsent) #joins the words with a space
print(splitjoin )
nl();
qouteception = "I said,\"give me all the money\"" #allows qoutes inside a qoutation
print(qouteception)
print("a" in"apple") #boolean (cases matter) should match syntax
print("\nStrip fun")
spaced = " hellloooo "
print(spaced)
print(spaced.strip()) #strip
print("\nReplace,Find string")
full_name = "Gahul Dravid"
print(full_name.replace("Gahul","Rahul"))
print(full_name.find("Dravid"))
print("\nPlaceholders")
movie = "Iron-Man"
print("My fav movie is {}.".format(movie))
nl();
#Dictionaries
print("Dictionaries : keys and values,uses curly braces{}")
drinks = {"frooti":10,"Maaza":12,"Appy":11} #drink name is key,price is value
print(drinks) #str-num.str-list..
drinks["Pepsi"] = 13 #adding key and value
print(drinks)
#importing
print("IMPORTING")
import sys #system functions and parameters
from datetime import datetime
print(datetime.now())
from datetime import datetime as dt #importing with as alias
print(dt.now())
def nl():
print("\n") #using fun nl to imply new lines easily
nl();
#Advanced Strings
print("Advanced Strings")
my_name = "illuminati"
print(my_name[0]) #first initial
print(my_name[-1]) #last initial
sentence = "This is a sentence"
print(sentence[:4]) #capturing the first word
print(sentence[-8:]) #capturing the last word
splitsent = sentence.split() #splits by space
print(splitsent)
splitjoin = " ".join(splitsent) #joins the words with a space
print(splitjoin )
nl();
qouteception = "I said,\"give me all the money\"" #allows qoutes inside a qoutation
print(qouteception)
print("a" in"apple") #boolean (cases matter) should match syntax
print("\nStrip fun")
spaced = " hellloooo "
print(spaced)
print(spaced.strip()) #strip
print("\nReplace,Find string")
full_name = "Gahul Dravid"
print(full_name.replace("Gahul","Rahul"))
print(full_name.find("Dravid"))
print("\nPlaceholders")
movie = "Iron-Man"
print("My fav movie is {}.".format(movie))
nl();
#Dictionaries
print("Dictionaries : keys and values,uses curly braces{}")
drinks = {"frooti":10,"Maaza":12,"Appy":11} #drink name is key,price is value
print(drinks) #str-num.str-list..
drinks["Pepsi"] = 13 #adding key and value
print(drinks)