Skip to content

Commit 04449c4

Browse files
Add files via upload
1 parent 74ca2e6 commit 04449c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+3876
-0
lines changed

Diff for: Ex39_Dictionary.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# create a mapping of state to abbreviation
2+
states = [
3+
'Oregon': 'OR',
4+
'Florida': 'FL',
5+
'California': 'CA',
6+
'New York': 'NY',
7+
'Michigan': 'MI'
8+
]
9+
10+
11+
# create a basic set of states and some cities in them
12+
cities = [
13+
'CA': 'San Francisco',
14+
'MI': 'Detroit',
15+
'FL': 'Jacksonville'
16+
]
17+
# add some more cities
18+
cities['NY'] = 'New York'
19+
cities['OR'] = 'Portland'
20+
21+
# print out some cities
22+
print '- ' * 10
23+
print "NY State has: ", cities['NY']
24+
print "OR State has: ", cities['OR']
25+
# print some states
26+
print '- ' * 10
27+
print "Michigan's abbreviation is: ", states['Michigan']
28+
print "Florida's abbreviation is: ", states['Florida']
29+
# do it by using the state then cities dict
30+
print '- ' * 10
31+
print "Michigan has: ", cities[states['Michigan']]
32+
print "Florida has: ", cities[states['Florida']]
33+
# print every state abbreviation
34+
print '- ' * 10
35+
for state, abbrev in states.items():
36+
print "%s is abbreviated %s" % (state, abbrev)
37+
# print every city in state
38+
print '- ' * 10
39+
for abbrev, city in cities.items():
40+
print "%s has the city %s" % (abbrev, city)
41+
# now do both at the same time
42+
print '- ' * 10
43+
for state, abbrev in states.items():
44+
print "%s state is abbreviated %s and has city %s" % (
45+
state, abbrev, cities[abbrev])
46+
print '- ' * 10
47+
# safely get an abbreviation by state that might not be there
48+
state = states.get('Texas', None)
49+
if not state:
50+
print "Sorry, no Texas."
51+
# get a city with a default value
52+
city = cities.get('TX', 'Does Not Exist')
53+
print "The city for the state 'TX' is: %s" % city

Diff for: challenge3.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
n = int(raw_input())
2+
3+
if n%2 != 0:
4+
print "Weird"
5+
elif n%2 == 0:
6+
if (n>=2 and n<=5):
7+
print "Not Weird"
8+
elif (n>=6 and n<=20):
9+
print "Weird"
10+
elif (n>20):
11+
print "Not Weird"

Diff for: challenge5.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a = int(raw_input())
2+
b = int(raw_input())
3+
print a/b
4+
print float(a)/b

Diff for: doubts.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Write at least one more function of your own design, and run it 10 different ways. pg 88
2+
filename.seek(0)
3+
open
4+
flush
5+
non blocking mode
6+
decorator expression (pydoc def)
7+
lambda expressions
8+
doubt test.py
9+
ex39
10+
ex42 last 3 comment left
11+
12+
13+
////////////////////////////////////////////
14+
15+
CODE aNALYSIS DOUBT
16+
-------------------
17+
18+

Diff for: ex1.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
print "hi "
2+
print "hello again"
3+
# print "I'm typing this" now this line a become a comment due to placing orthoscope '#'
4+
print "hello i m typing a line again"
5+

Diff for: ex10_Escape.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tabby_cat = "\tI'm tabbed in."
2+
persian_cat = "I'm split\non a line."
3+
backslash_cat = "I'm \\ a \\ cat."
4+
fat_cat = """
5+
I'll do a list:
6+
\t* Cat food
7+
\t* Fishies
8+
\t* Catnip\n\t* Grass
9+
"""
10+
print tabby_cat
11+
print persian_cat
12+
print backslash_cat
13+
print fat_cat
14+
15+
print '\n \t %r'%'\'\t "'
16+
print '\n %s'%'\' "'
17+
18+
19+
print "\n %r"%"' \""
20+
print "\n %s"%"' \""
21+

Diff for: ex11_inputs.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
print "how old are you?",
2+
age=raw_input()
3+
print "what is your weight?",
4+
weight=raw_input()
5+
print "how tall are you?",
6+
tall=raw_input()
7+
print "hey i want to ask some more question"
8+
print"do you know about stuxnet",
9+
10+
#Notice that we put a , (comma) at the end of each print line. This is so that
11+
#print doesn’t end the line with a new line character and go to the next line.

Diff for: ex12_PromptedInputs.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
age = raw_input("How old are you? ")
2+
height = raw_input("How tall are you? ")
3+
weight = raw_input("How much do you weigh? ")
4+
5+
print "So, you're %r old, %r tall and %r heavy." % (
6+
age, height, weight)
7+

Diff for: ex13_arguments.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from sys import argv
2+
script, first, second, third = argv
3+
print "The script is called:", script
4+
print "Your first variable is:", first
5+
print "Your second variable is:", second
6+
print "Your third variable is:", third
7+

Diff for: ex14_PromptAndPassing.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
from sys import argv
3+
script, user_name = argv
4+
prompt = '> '
5+
print "Hi %s, I'm the %s script." % (user_name, script)
6+
print "I'd like to ask you a few questions."
7+
print "Do you like me %s?" % user_name
8+
likes = raw_input(prompt)
9+
print "Where do you live %s?" % user_name
10+
lives = raw_input(prompt)
11+
print "What kind of computer do you have?"
12+
computer = raw_input(prompt)
13+
print """
14+
Alright, so you said %r about liking me.
15+
You live in %r. Not sure where that is.
16+
And you have a %r computer. Nice.
17+
""" % (likes, lives, computer)

Diff for: ex15_ReadingFiles.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
from sys import argv #import argv from the sys module
3+
script, filename = argv #defines script and filenames are arguments
4+
txt = open(filename) #calls a method open on the file passed as an argument
5+
print "Here's your file %r:" % filename #print the filename
6+
print txt.read() #call the read command on the file being passed as an argument
7+
txt.close()
8+
print "Type the filename again:" # simple print statement
9+
file_again = raw_input("> ") #again ask name of the file
10+
txt_again = open(file_again) #call open method on the file name which is passed recently
11+
print txt_again.read() #call read method on the file being passedrecently
12+
txt_again.close()
13+

Diff for: ex15_sample.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is stuff I typed into a file.
2+
It is really cool stuff.
3+
Lots and lots of fun to have in here.

Diff for: ex16_rnWrite.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from sys import argv
2+
script, filename = argv
3+
print "this is your script name %r" % script
4+
print "We're going to erase %r." % filename
5+
print "If you don't want that, hit CTRL- C (^C)."
6+
print "If you do want that, hit RETURN."
7+
raw_input("?")
8+
print "Opening the file..."
9+
target1 = open(filename)
10+
target2 = target1.read()
11+
print "raw data in file \n %r" % target2
12+
print "\n\n\n\n"
13+
print "formatted data in file %s"%target2
14+
target1.close()
15+
target = open(filename, 'w')
16+
print "Truncating the file.Goodbye!"
17+
target.truncate()
18+
print "Now I'm going to ask you for three lines."
19+
line1 = raw_input("line 1: ")
20+
line2 = raw_input("line 2: ")
21+
line3 = raw_input("line 3: ")
22+
print "I'm going to write these to the file."
23+
target.write(line1)
24+
target.write("\n")
25+
target.write(line2)
26+
target.write("\n")
27+
target.write(line3)
28+
target.write("\n")
29+
print "And finally, we close it."
30+
target.close()

Diff for: ex16_rnw2.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
from sys import argv
3+
script , filename = argv
4+
print "This Program wil erase all contennt of selected file %r"%filename
5+
print "To Continue press ENTER and to Quit press "
6+
raw_input("?")
7+
print "opening file"
8+
target=open(filename, 'w')
9+
target.truncate()
10+
11+
print "The Content of %r file deleted"%filename
12+
print "Now this part of program will attempt to rewrite the contents back into the file"
13+
print "To Continue press ENTER and to Quit press "
14+
raw_input("?")
15+
16+
line1 = raw_input("Line 1: ")
17+
line2 = raw_input("Line 2: ")
18+
line3 = raw_input("Line 3: ")
19+
line = line1+"\n"+line2+"\n"+line3
20+
print "\n Now we gonna write these lines in you file using write method"
21+
target.write(line1+"\n"+line2+"\n"+line3)
22+
# target.write(line)
23+
24+
print "Now we close the file"
25+
target.close()
26+

Diff for: ex16_sample.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
hi
3+
bye

Diff for: ex17_from.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hi i m a from filee

Diff for: ex17_morefiles.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from sys import argv
2+
from os.path import exists
3+
4+
script , fromfile, tofile = argv
5+
6+
print "copying file %s from %s" % ( tofile, fromfile)
7+
8+
in_file = open(fromfile, 'r')
9+
indata = in_file.read()
10+
11+
print "the input file is %d bytes long" % len(indata)
12+
print "does the output file exist %r" % exists(tofile)
13+
14+
print "press ctrl+c to quit the program or Enter to continue"
15+
raw_input("?")
16+
17+
out_file = open(tofile, 'w')
18+
out_file.write(indata)
19+
20+
print "Allright Alldone !"
21+
22+
in_file.close
23+
out_file.close
24+
25+

Diff for: ex17_short.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from sys import argv
2+
script , fromfile, tofile = argv
3+
(open(tofile, 'w')).write(((open(fromfile, 'r')).read()))
4+

Diff for: ex17_to.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hi i m a from filee

Diff for: ex18_Function.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# this one is like your scripts with argv
2+
def print_two(*args):
3+
arg1, arg2 = args
4+
print "arg1: %r arg2: %r" % (arg1, arg2)
5+
6+
# ok, that *args is actually pointless, we can just do this
7+
8+
def print_two_again(arg1, arg2):
9+
print "arg1: %r arg2: %r" % (arg1, arg2)
10+
11+
# this will only take one argument
12+
13+
def print_one(arg1):
14+
print "arg1: %r" % arg1
15+
16+
# this will print none as no arguments
17+
18+
def print_no():
19+
print "i'm not gonna do anything"
20+
21+
print_two('zed', 'shaw')
22+
print_two_again('abhishek', 'gautam')
23+
print_one('abhishekGautam')
24+
print_no()
25+

Diff for: ex19_Func2.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def cnc(chesecount, crackercount):
2+
print "The chese count is %d" % chesecount
3+
print "The Cracker count is %d" % crackercount
4+
print "that's enfough for party man"
5+
6+
cnc(10, 20)
7+
8+
amt_chese=20
9+
amt_cracker=30
10+
11+
print "we can pass parameter in form of varibale"
12+
cnc(amt_chese, amt_cracker)
13+
14+
print "we can even do maths while passing the argument"
15+
16+
cnc(10+30, 30+40)
17+
18+
print "we can combine both variables and maths"
19+
20+
cnc(amt_chese+10, amt_cracker+20)
21+
22+

Diff for: ex20_funcnFile.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from sys import argv
2+
script, input_file = argv
3+
def print_all(f):
4+
print f.read()
5+
def rewind(f):
6+
f.seek(0)
7+
def print_a_line(line_count, f):
8+
print line_count, f.readline()
9+
current_file = open(input_file)
10+
print "First let's print the whole file:\n"
11+
print_all(current_file)
12+
print "Now let's rewind, kind of like a tape."
13+
rewind(current_file)
14+
print "Let's print three lines:"
15+
current_line = 1
16+
print_a_line(current_line, current_file)
17+
current_line += 1
18+
print_a_line(current_line, current_file)
19+
current_line += 1
20+
print_a_line(current_line, current_file)

Diff for: ex20_sample.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
hello
2+
this
3+
is
4+
a
5+
multi
6+
line
7+
document
8+

Diff for: ex21_fcontd.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def add(a, b):
2+
print "ADDING %d + %d" % (a, b)
3+
return a + b
4+
def subtract(a, b):
5+
print "SUBTRACTING %d - %d" % (a, b)
6+
return a - b
7+
def multiply(a, b):
8+
print "MULTIPLYING %d * %d" % (a, b)
9+
return a * b
10+
def divide(a, b):
11+
print "DIVIDING %d / %d" % (a, b)
12+
return a / b
13+
print "Let's do some math with just functions!"
14+
age = add(30, 5)
15+
height = subtract(78, 4)
16+
weight = multiply(90, 2)
17+
iq = divide(100, 2)
18+
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
19+
# A puzzle for the extra credit, type it in anyway.
20+
print "Here is a puzzle."
21+
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
22+
print "That becomes: ", what, "Can you do it by hand?"

0 commit comments

Comments
 (0)