Exercise 2 - 3
In [1]:
def print_two(*args):
arg1, arg2 = args
print ("arg1: %r, arg2: %r" % (arg1, arg2))
print_two ("Siva", "Soham")
In [7]:
def print_two_again(arg1, arg2):
print ("arg1: %r, arg2: %r" % (arg1, arg2))
print_two_again ("Siva", "Soham")
In [9]:
def print_one(arg1):
print ("arg1: %r" % arg1)
print_one ("Siva and Soham")
In [11]:
def print_none():
print ("I got somethin'.")
print_none ()
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))
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))
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)
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)
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))
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
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"
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")
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")
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!")
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)
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)
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)
In [4]:
Height = [70,73,24,54,67]
for soham in Height:
print ("MY HEIGHT IS %d" %soham )
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
Post a Comment