Exercise 2 - 3


PY_EX_1_2
In [1]:
def print_two(*args):
        arg1, arg2 = args
        print ("arg1: %r, arg2: %r" % (arg1, arg2))
        
print_two ("Siva", "Soham")
arg1: 'Siva', arg2: 'Soham'
In [7]:
def print_two_again(arg1, arg2):
    print ("arg1: %r, arg2: %r" % (arg1, arg2))
    
print_two_again ("Siva", "Soham")
arg1: 'Siva', arg2: 'Soham'
In [9]:
def print_one(arg1):
    print ("arg1: %r" % arg1)
    
print_one ("Siva and Soham")
arg1: 'Siva and Soham'
In [11]:
def print_none():
    print ("I got somethin'.")
    
print_none ()
I got somethin'.
In [12]:
def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print ("You have %d cheeses!" % cheese_count)
    print ("You have %d boxes of crackers!" % boxes_of_crackers)
    
    
print (cheese_and_crackers (20,30))
    
    
You have 20 cheeses!
You have 30 boxes of crackers!
None
In [2]:
amount_of_cheese = 10
amount_of_crackers = 50


def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print ("You have %d cheeses!" % cheese_count)
    print ("You have %d boxes of crackers!" % boxes_of_crackers)

print (cheese_and_crackers(10 + 20, 5 + 6))

print (cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000))
You have 30 cheeses!
You have 11 boxes of crackers!
None
You have 110 cheeses!
You have 1050 boxes of crackers!
None
In [12]:
from sys import argv

script, input_file = argv,"untitled.txt"


def print_all(f):
    print (f.read())

def rewind(f):
    f.seek(0)

def print_a_line(line_count, f):
    print (line_count, f.readline())
    
current_file = open(input_file)


print ("First let's print the whole file:\n")

print_all(current_file)

rewind(current_file)

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
First let's print the whole file:

i am soham
asbsjahj
shajkjajksa
1 i am soham

2 asbsjahj

3 shajkjajksa
In [20]:
def add(a, b):
    print ("ADDING %d + %d" % (a, b))
    return a + b

def subtract(a, b):
    print ("SUBTRACTING %d - %d" % (a, b))
    return a - b

def divide(a, b):
    print ("DIVIDING %d / %d" % (a, b))
    return a / b

age = add(30, 5)
height = subtract(78,4)
iq = divide(100, 2)

print (age)
print (height)
print (iq)
ADDING 30 + 5
SUBTRACTING 78 - 4
DIVIDING 100 / 2
35
74
50.0
In [1]:
#List Of Symbols
#Note Python is case sensitive.... 
#How to Use Print Function ----- print("How are you")
#Use "#" to comments and Pounds
#Arithmetic Operations like how to use +, -, *, /, % >, <, >=,<=) 
   # Eg: print ("My_weight=", 30+20)
   # Eg: print (20+50*52/2)
#we and assign value to string eg: Car=20

#%s---> for String
#%d ---> for Numbers
#%r ---> for debugging
#"." ---> use this for end a paragraph or print statement.
#\n is for next line 
#\t is for tab
#\\ -- make something special :) i'm\a\cat
#input - used to give input value
In [2]:
print ("Lets Practice everything")
print ('I\'m data analyst \\ Cool Right! \n what Next \t ?????.')
Conversation= """
Siva- Hi Bro
Good Morning
Soham - Hey shiv
\n Good Morning
\t siva- what's the plan today
"""
print(Conversation)

Math = 10+20*5/2 

print ("Ans for math problem is: %r" % Math)

def secret_formula (started):
    jelly_bean = started *500
    jars = jelly_bean / 1000
    crates =jars /100
    return jelly_bean,jars,crates

start_point =10000
beans, jars,crates = secret_formula(start_point)
print ("starting point is: %d" % start_point)
print ("I got  have %d beans, %d jars, and %d crates." % (beans, jars, crates))

print ("Second method--> we have %d beans, %d jars, and %d crates." % secret_formula(start_point))
Lets Practice everything
I'm data analyst \ Cool Right! 
 what Next 	 ?????.

Siva- Hi Bro
Good Morning
Soham - Hey shiv

 Good Morning
	 siva- what's the plan today

Ans for math problem is: 60.0
starting point is: 10000
I got  have 5000000 beans, 5000 jars, and 50 crates.
Second method--> we have 5000000 beans, 5000 jars, and 50 crates.
In [4]:
#EX-25

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print (word)
    
def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print (word)
    
    
def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words) 

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)
In [4]:
import PYEX25

sentence = "All good things come to those who wait."
words = PYEX25.break_words(sentence)
words

sorted_words = PYEX25.sort_words(words)
sorted_words

first_word = PYEX25.print_first_word(words)
first_word

last_word = PYEX25.print_last_word(words)
last_word
All
wait.
In [1]:
1. True and True #True
2. False and True #False
3. 1 == 1 and 2 == 1 #False 
4. "test" == "test" #True
5. 1 == 1 or 2 != 1 #True
6. True and 1 == 1 #True
7. False and 0 != 0 #False
8. True or 1 == 1 #True
9. "test" == "testing" #False
10. 1 != 0 and 2 == 1 #False
11. "test" != "testing" #True
12. "test" == 1 #False
13. not (True and False) #True
14. not (1 == 1 and 0 != 1) #True
15. not (10 == 1 or 1000 == 1000) #False
16. not (1 != 10 or 3 == 4) #False
17. not ("testing" == "testing" and "Zed" == "Cool Guy") #True
18. 1 == 1 and not ("testing" == 1 or 1 == 0) #True
19. "chunky" == "bacon" and not (3 == 4 or 3 == 3) 
20. 3 == 3 and not ("testing" == "testing" or "Python" == "Fun"
  File "<ipython-input-1-20162842e994>", line 1
    1. True and True #True
          ^
SyntaxError: invalid syntax
In [1]:
#EX29

People = 20
Cats = 30
Dogs = 15

if People < Cats:
    print ("Many cats and Less People")
    
if People > Dogs:
    print ("There are more people thn Dogs")
    
Dogs += 5

if People == Dogs:
    print ("Wow Same Population of Dog and People")
    
if Cats != Dogs:
    print ("No equal number of dogs and Cats")
    
if Cats < Dogs:
    print (" Less cats ! Hahaha")
Many cats and Less People
There are more people thn Dogs
Wow Same Population of Dog and People
No equal number of dogs and Cats
In [2]:
Apple = 50
Orange = 40
Graps = 35

if Apple < Orange:
    print ("Orange is Costly")
elif Apple > Orange:
    print ("Apple is Costly")
else:
    print ("Both are equal")
    
if Orange > Graps:
    print ("Orange is Costly")
else:
    print ("Graps is Costly")
    
Orange += 10

if Apple > Orange:
    print ("Apple is Costly")
    
elif Apple < Orange:
    print ("Orange is Costly")
    
elif Apple != Orange:
    print("Apple and Orange are same Cost")
    
elif Apple == Orange:
    print ("Both cost same")
    
else:
    ("What's the Price of Apple and Orange")
Apple is Costly
Orange is Costly
Both cost same
In [3]:
print ("You enter the dark room with two doors. Do you get in to door #1 or #2?") 

door = input (">")

if door == "1":
    print ("There is a monster. what do you do")
    print ("1.Take the cake")
    print ("2.Scream the Bear.")
    
    bear = input (">")
    if bear == "1":
        print ("The bear eats you face")
    elif bear == "2":
        print ("The bear eats your legs off")
    else:
        print ("Well done %s is probably better. Bear run away" % bear)
        
elif door == "2":
    print ("You stare into the endless abyss at Cthuhlu's retina.")
    print ("1. Blueberries.")
    print ("2. Yellow jacket clothespins.")
    print ("3. Understanding revolvers yelling melodies.")
    
    insanity = input (">")
    if insanity == "1" or insanity == "2":
        print ("Your body survives powered by a mind of jello. Good job!")
    else:
        print("The insanity rots your eyes into a pool of muck. Good job!")

else:
    print("You stumble around and fall on a knife and die. Good job!")
        
        
You enter the dark room with two doors. Do you get in to door #1 or #2?
>1
There is a monster. what do you do
1.Take the cake
2.Scream the Bear.
>1
The bear eats you face
In [4]:
#EX 32

hairs =['Brown', 'Black', 'Red']
eyes = ['brown', 'black', 'green']
weight= [1, 2, 3, 4]

for hair_color in hairs:
    print ("Color of my hair is %s:" % hair_color)
    
for eye_color in eyes:
    print ("\nColor of my eye is %s:" % eye_color)

for weights in weight:
    print ("My Weight is %d Kg" % weights)
    
# creating a list

elements = []

for i in range (0,6):
    print ("Adding %d to the list" % i)
    elements.append(i)
    
for i in elements:
    print ("Elements are %d" % i)
Color of my hair is Brown:
Color of my hair is Black:
Color of my hair is Red:

Color of my eye is brown:

Color of my eye is black:

Color of my eye is green:
My Weight is 1 Kg
My Weight is 2 Kg
My Weight is 3 Kg
My Weight is 4 Kg
Adding 0 to the list
Adding 1 to the list
Adding 2 to the list
Adding 3 to the list
Adding 4 to the list
Adding 5 to the list
Elements are 0
Elements are 1
Elements are 2
Elements are 3
Elements are 4
Elements are 5
In [5]:
i=0
number =[]

while i<6:
    print ("At the top i is %d" % i)
    number.append(i)
    
    i=i+1
    print ("Numbers now:", number)
    print ("At the bottom i is %d" % i)
    
print ("The Numbers")

for num in number:
    print (num)
At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The Numbers
0
1
2
3
4
5
In [1]:
Z=0
age=[]

while Z<=10:
    print ("My age is %d" % Z)
    age.append(Z)
    
    Z=Z+1
    print("Now my age is:", age)
    print ("I'm gonna get to age %d next year" % Z)
My age is 0
Now my age is: [0]
I'm gonna get to age 1 next year
My age is 1
Now my age is: [0, 1]
I'm gonna get to age 2 next year
My age is 2
Now my age is: [0, 1, 2]
I'm gonna get to age 3 next year
My age is 3
Now my age is: [0, 1, 2, 3]
I'm gonna get to age 4 next year
My age is 4
Now my age is: [0, 1, 2, 3, 4]
I'm gonna get to age 5 next year
My age is 5
Now my age is: [0, 1, 2, 3, 4, 5]
I'm gonna get to age 6 next year
My age is 6
Now my age is: [0, 1, 2, 3, 4, 5, 6]
I'm gonna get to age 7 next year
My age is 7
Now my age is: [0, 1, 2, 3, 4, 5, 6, 7]
I'm gonna get to age 8 next year
My age is 8
Now my age is: [0, 1, 2, 3, 4, 5, 6, 7, 8]
I'm gonna get to age 9 next year
My age is 9
Now my age is: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
I'm gonna get to age 10 next year
My age is 10
Now my age is: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I'm gonna get to age 11 next year
In [4]:
Height = [70,73,24,54,67]

for soham in Height:
    print ("MY HEIGHT IS %d" %soham )
MY HEIGHT IS 70
MY HEIGHT IS 73
MY HEIGHT IS 24
MY HEIGHT IS 54
MY HEIGHT IS 67
In [ ]:
# Exercise 34: Accessing Elements Of Lists

# Remember: ordinal == ordered, 1st; cardinal == cards at random, 

# Simple, every time you say to yourself, “I want the 3rd animal,”
# you translate this “ordinal” number to a “cardinal” number by subtracting 1.
# The “3rd” animal is at index 2 and is the penguin.
# You have to do this because you have spent your whole life using ordinal numbers,
# and now you have to think in cardinal.
# Just subtract 1 and you will be good.

animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus']

# 1. The animal at 1 # Python
# 2. The 3rd animal. #Peacock
# 3. The 1st animal. #Bear
# 4. The animal at 3. #Kangaroo
# 5. The 5th animal. #Whale
# 6. The animal at 2. #Peacock
# 7. The 6th animal. #Platypus
# 8. The animal at 4. #Whale

Comments

Popular posts from this blog

Pandas Library

Matplot LIbrary