Wednesday, October 24, 2018

Python Scripts



Script1:
print "I will now count my chickens:"
print "Hens", 25 + 30 / 6
print "Rossters", 100 - 25 * 3 % 4

print "How I will count the eggs:"

print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

print "Is it true that 3 + 2 < 5 - 7?"

print 3 + 2 < 5 -7

print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7

print "oh, that's why it's False."

print "How about some more."

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2

Script2 :
print "Hello world!"
#print "Hello Again!"
#print "I like typing this."
#print "This is fun."
#print 'Yay! Printing.'
#print "I'd much rather you 'not'."
#print 'I "said" do not touch this.'
#print '"another line"'

Script3 :
# divide a floating point number by anothe floating point number
print 10.5/2.12

# Divide one floating point with antoher floating. Without dropping gractions
print 7.0 / 4.0

# Divide one integr with another integer. Fraction drops.
print 7/4

Script4:
cars = 100
space_in_a_car = 4
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print "There are ", cars, "cars available"
print "There are only", drivers, "drivers available"
print "There will be ", cars_not_driven, "empty cars today"
print "We can transport", carpool_capacity, "peole today"
print "web have", passengers, "to carpool today"
print "We need to put about", average_passengers_per_car, "in each car"

Script5 :
# exercise 5 - More strings

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 #lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounts heavy" % my_weight
print "Actully that's not too heavy"
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth

# this line is tricky, try to get it exactly right

print "if I add %d, %d, and %d I get %d" % (my_age, my_height, my_weight, my_age + my_height + my_weight)

print "this is %r nothing new %r in my line of %r work" % (my_age, my_height, my_weight)

Script6 :
# learn python the hardway. Exercise 6
x = "There are %d types of people" % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s" % (binary, do_not)

print x
print y

print "I said: %r." % x
print "I also said: '%s'." % y

hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"

print joke_evaluation % hilarious

w = "This is the left side of ...."
e = "a string with a right side"

print w + e

Script7 :
# learn python the hardway . Exercise 7
print "Mary had a little lamp"
print " Its fleece was whit as %s." % 'snow'
print "And everywhere that Mary went."
print "." * 10 #what'd that do ?

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"


# watch that comma at the end. try removing it and see what happens

print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12

Script8:
# learn python the hardway. Exercise 8
formatter = "%r %r %r %r"

print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "Three", "Four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
    "I had this thing",
    "That you could type up right.",
    "But it didn't sing",
    "So I said goodnight."
)

Script9:
# Learn python the hard way. Exercise 9

# Here's some new strange stuff, remember type it exactly

days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"

print "Here are the days: ", days
print "Here are the months: ", months

print """
There's something going on here.
With the thre double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""

Script10 :
while True:
  for i in ["/","-","|","\\","|"]:
    print "%s\r" %i,

Script 11 :
# Learn python the hard way . Exercise 10
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line"
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishes
\t* Catnip\n\t* Grass
"""

print tabby_cat
print persian_cat
print backslash_cat
print fat_cat

Script12 :
# Learn python the hard way. Exercise 11
print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()

print "so, you're %r old, %r tall and %r heavy." %(
        age,height,weight)

Script 13 :
# Learn Python The Hard Way . Exercise 12
age = raw_input("How old are you? ")
height = raw_input("How tall are you?")
weight = raw_input("How much do you weigh?")

print "So, you're %r old, %r tall and %r heavy" %(
    age,height,weight)    

Script 14 :
# Learn Python the Hard Way. Exercise 13

from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "Yoru first variable is :", first
print "Your second variable is:", second
print "Your third variable is :", third


Script 15 :
# Learn Python The Hard Way. Exercise 14
from sys import argv

script, user_name = argv
prompt = '####### '

print "Hi %s, I'm the %s script." %(user_name, script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes = raw_input(prompt)

print "Where do you live %s?" % user_name
lives = raw_input(prompt)

print "What kind of computer do you have ?"
computer = raw_input(prompt)

print """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is .
And you have a %r computer. Nice.
""" % (likes, lives, computer)

Script 16 :
Divya.txt
[
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
]


# Learn Python the Hard Way. Exercise 15

from sys import argv

script, filename = argv
txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

close(txt)
# read the input file name again from user.
print "Type the filename again:"
file_again = raw_input("> ")

# Open the file for reading
txt_again = open(file_again)

# read the opened file and print it.
print txt_again.read()
close(txt_again)

Script 17 :
# Learn python the hard way. Exercise 16-1

# this script , reads the file test.txt that was created
# in the last exercise.

# collect the file name in the argv from user

from sys import argv

script, filename = argv

#open the file for reading
myfile = open(filename)

#read the file & print it
print myfile.read()

myfile.close()

Script 18 :
# Learn Python The Hard Way. Exercise 16
# import module argv from package sys
from sys import argv

#unpack the argv from the user input into variables
script,filename = argv

#print the warning message to the user
print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

#
raw_input("?")

print "Opening the file ..."
target = open(filename, 'w')

print "Truncating the file. Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file"

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

#
print "And finally, we close it. "
target.close()

Script 19 :
# Learn python the hard way. Exercise 17

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)
# read the data from first file
in_file = open(from_file)
indata = in_file.read()


# write it to the second file, if exists
print "Does the output file exists? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort"
raw_input

out_file = open(to_file, 'w')
out_file.write(indata)


# close both files
out_file.close()
in_file.close()

Script 20 :
# Learn python the hard way. Exercise 17

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)
# read the data from first file
in_file = open(from_file)
indata = in_file.read()

print "The input file is %d bytes long" % len(indata)

# write it to the second file, if exists
print "Does the output file exists? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort"
raw_input

out_file = open(to_file, 'w')
out_file.write(indata)

print " Alright, all done."
# close both files
out_file.close()
in_file.close()

Script 21 :
# Learn Python the hard way. Exercise 18
# this one is like your scripts iwth argv

def print_two(*args):
    arg1, arg2 = args
    print "arg1: %r, arg2: %r" % (arg1, arg2)

#ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
    print "arg1: %r, arg2: %r" % (arg1, arg2)

# this just takes one argment
def print_one(arg1):
    print "arg1: %r" % arg1

# this one takes no arguments
def print_none():
    print "I got nothin'."

print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()

Script 22 :
# Learn python the hard way. Exercise 19
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 "Man that's enough for a party!"
    print "Get a blanket.\n"


print "We can just give the function numbers directly:"
cheese_and_crackers(20,30)

print "OR, we can use variables from our script"
amount_of_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, amount_of_crackers)

print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)

print "And we can combile the two, varibales and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

script 23 :
#Learn Python the Hard Way. Exercise 20

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

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

def multiply(a, b):
    print "MULTYPLYING %d * %d" % (a, b)
    return a * b

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

print "Let's do some math with just functions"

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

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)

# A puzzle for the exra credit, type it in anyway.
print "Here is a puzzle."

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Cana you do it by hand?"

Script 24 :
# Learn python the hard way. Exercise 24
print "let's practice everything"
print "You\'d need to know \'bout escaping with \\ that do \n newlines and \t tabs."

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intition
and requires an explantion
\n\t\twhere there is none.
"""

print "---------------"
print poem
print "---------------"

five = 10 - 2 + 3 - 6
print "This should be file: %s" % five

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

start_point = 10000
beans, jars, crates = secret_formula(start_point)

print "With a starting poing of : %d" % start_point
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)


start_point =  start_point / 10

print "We can also do that this way"
print "We'd have %d beans, %d jars, and %d crates" % secret_formula(start_point)

script 25 :
# Learn Python the hard way. Exercise 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 and 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)

mystuff = "How can i show how much i love you"
break_words(mystuff)

script 26 :
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)


print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""


print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 - 5
print "This should be five: %s" % five

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


start_point = 10000
beans, jars, crates = secret_formula(start_point)

print "With a starting point of: %d" % start_point
print "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)

start_point = start_point / 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)


sentence = "All god\tthings come to those who weight."

words = break_words(sentence)
sorted_words = sort_words(words)

print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = sort_sentence(sentence)
print sorted_words

print_first_and_last(sentence)
print_first_and_last_sorted(sentence)

Script 27 :
# Learn Python the Hard Way. Exercise 29

people = 20
cats = 30
dogs = 15

if people < cats:
    print "Too many cats! The world is doomed!"

if people > cats:
    print "Not many cats! The word is saved"

if people < dogs:
    print "The world is drooled on!"

if people > dogs:
    print "The world is dry!"

dogs += 5

if people > dogs:
    print "People are greated than or equal to dogs."

if people <= dogs:
    print "People are less than or equal to dogs"

if people == dogs:
    print "People are dogs"    

Script 28 :
# Learn Python the hard way. Exercise 30
people = 30
cars = 40
trucks = 15

if cars > people:
    print "We should take the cars"
elif cars < people:
    print "We should not take the cars"
else:
    print "We cant decide"

if trucks > cars:
    print "That's too many tructs"
elif trucks < cars:
    print "may we could take the trucks"
else:
    print "We still cant decide"

if people > trucks:
    print "Alright, let's just take the trucks"
else:
    print "Fine, let's stay home then."

Script 29 :
# Learn python the hard way . Exercise 31
print "You enter a dark room with two doors. do you go through door # 1 or door #2 ?"

door = raw_input("> ")

if door == "1":
    print "There's a gaint bear here eating a cheese cake. What do you do? "
    print "1. Take the cake."
    print "2. Scream at the bear."

    bear = raw_input("> ")

    if bear == "1":
        print "The bear eats your face off. Good job!"
    elif bear == "2":
        print "The bear eats your legs off. Good job!"
    else:
        print "Well, doing %s is probably better. Bear run" % bear


elif door == "2":
    print "You stare into the endless abyss at Cthylhu's retina"
    print "1. Blueberries."
    print "2. Yellow jacket clothespins."
    print "3. Understanding revolvers yelling melodies."

    insanity = raw_input("> ")

    if insanity == "1" or insanity == "2":
        print "Your body survives powered by a mind of jello. Good job"

    else:
        print "The insanity rots yur eyes into a pool of muck. Good job"

else:
    print "You stumble around and fall on a knife and die. Good job"


Script 30 :
# Learn python the hard way. Exercise 31

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'domes', 3, 'quarters']

# this first kind of for-loop goes through a list

for number in the_count:
    print "This is cound %d" % number

# same as above
for fruit in fruits:
    print "A fruit of type: %s" % fruit

# also we can go through mixes lists too
# notice we have to use %r since we dont know what's in it
for i in change:
    print "I got %r" % i

# we can also build lists; first start with an empty one

elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print "Adding %d to the list." % i

    # append is a function that lists Understand
    elements.append(i)

# now we can print them out too

for i in elements:
    print "Element was: %d" %i

Script 31 :
# Learn python the Hard Way. Exercise 33

i = 0
numbers = []

limit = 6
while i < limit:
    print "At the top i is %d" % i
    numbers.append(i)

    i = i + 1
    print "Numbers now: ", numbers
    print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
    print num

Script 32 :
# Learn Python the Hard Way. Exercise 39

# create a mapping of state to abbreviation

states = {
    'Oregon': 'OR',
    'Florida': 'FL',
    'California': 'CA',
    'New York': 'NY',
    'Michigan': 'MI',
}

# create a basic set of states and some citities in them

cities = {
    'CA': 'San Francisco',
    'MI': 'Detroit',
    'FL': 'Jacksonville',
}

#add some more citities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'

# print out some cities
print '-' * 10
print "NY State has: ", cities['NY']
print "OR State has: ", cities['OR']

# print some states
print '-' * 10
print "Michigan's abbreviation is : ", states['Michigan']
print "Florida's addreviation is: ", states['Florida']

# do it by using the state then cities dict
print '-' * 10
print "Michigan has: ", cities[states['Michigan']]
print "Florida has: ", cities[states['Florida']]

# print every sate abbreviation
print '-' * 10
for state, abbrev in states.items():
    print "%s is abbreviated %s" % (state, abbrev)

# print every city in state
print '-' * 10
for abbrev, city in cities.items():
    print "%s has the city %s" % (abbrev, city)

#now do bothat the same time
print "-" * 10
for stae,abbrev in states.items():
    print "%s state is abbreviated %s and has city %s" % (
    state,abbrev,cities[abbrev])

print '-' * 10
# safely get a abbreviation by state that might not be there
state = states.get("Texas")

if not state:
    print "sorry, no Texas"

#get a city with a default value

city = cities.get('TX', 'Does not Exist')
print "The city for the sate 'TX' is : %s" % city

Script 33 :
# Learn Python the hard way. Exercise 40

class Song(object):

    def __init__(self,lyrics):
        self.lyrics = lyrics

    def sing_me_a_song(self):
        for line in self.lyrics:
            print line


lyrics1 = ["New Lyrics", "How are the lyrics1 ", "Isnt it beautifual lyrics1"]
happy_bday = Song(["Happy birthday to you",
                    "I don't want to get sued",
                    "So I'll stop right there"])

happy_bday1 = Song(lyrics1)

bulls_on_parade = Song(["They rally around tha family",
                        "With pockets full of shells"])

happy_bday.sing_me_a_song()
happy_bday1.sing_me_a_song()
bulls_on_parade.sing_me_a_song()

Script 34 :
# Learn Python The Hard Way. Exercise 41
import random
from urllib import urlopen
import sys

WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []

PHRASES = {
    "class %%%(%%%):":
      "Make a class named %%% that is-a %%%.",
    "class %%%(object):\n\tdef __init__(self, ***)" :
      "class %%% has-a __init__ that takes self and *** parameters.",
    "class %%%(object):\n\tdef ***(self, @@@)":
      "class %%% has-a function named *** that takes self and @@@ parameters.",
    "*** = %%%()":
      "Set *** to an instance of class %%%.",
    "***.***(@@@)":
      "From *** get the *** function, and call it with parameters self, @@@.",
    "***.*** = '***'":
      "From *** get the *** attribute and set it to '***'."
}

# do they want to drill phrases first
if len(sys.argv) == 2 and sys.argv[1] == "english":
    PHRASE_FIRST = True
else:
    PHRASE_FIRST = False

# load up the words from the website
for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.strip())


def convert(snippet, phrase):
    class_names = [w.capitalize() for w in
                   random.sample(WORDS, snippet.count("%%%"))]
    other_names = random.sample(WORDS, snippet.count("***"))
    results = []
    param_names = []

    for i in range(0, snippet.count("@@@")):
        param_count = random.randint(1,3)
        param_names.append(', '.join(random.sample(WORDS, param_count)))

    for sentence in snippet, phrase:
        result = sentence[:]

        # fake class names
        for word in class_names:
            result = result.replace("%%%", word, 1)

        # fake other names
        for word in other_names:
            result = result.replace("***", word, 1)

        # fake parameter lists
        for word in param_names:
            result = result.replace("@@@", word, 1)

        results.append(result)

    return results


# keep going until they hit CTRL-D
try:
    while True:
        snippets = PHRASES.keys()
        random.shuffle(snippets)

        for snippet in snippets:
            phrase = PHRASES[snippet]
            question, answer = convert(snippet, phrase)
            if PHRASE_FIRST:
                question, answer = answer, question

            print question

            raw_input("> ")
            print "ANSWER:  %s\n\n" % answer
except EOFError:
    print "\nBye"

No comments:

Post a Comment