Quantcast
Channel: sunny
Viewing all articles
Browse latest Browse all 44

[Python] codecademy-20140317

$
0
0

+keys() and values()

my_dict = {
    "Name": "Jong-seon Kim",
    "Age": 30,
    "Address": "Seoul"
}

# returns an array of the dictionary's keys
print my_dict.keys()

# returns an array of the dictionary's values
print my_dict.values()

+The ‘in’ operator

my_dict = {
    "Name": "Jong-seon Kim",
    "Age": 30,
    "Address": "Seoul"
}

# returns an array of the dictionary's keys
print my_dict.keys()

# returns an array of the dictionary's values
print my_dict.values()

print
for key in my_dict:
    print key, " ", my_dict[key]

+ list comprehension syntax

doubles_by_3 = [x*2 for x in range(1,6) if (x*2) % 3 == 0]

# Complete the following line. Use the line above for help.
even_squares = [i**2 for i in range(1,11) if i%2 == 0]

print even_squares

+cubes_by_four

cubes_by_four = [i**3 for i in range(1,11) if i**3%4 == 0]

print cubes_by_four

+ list slicing syntax

l = [i ** 2 for i in range(1, 11)]
# Should be [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

# [start:end:stride]
# start (inclusive)
# end (exclusive)
# stride - the space between items in the sliced list
print l[2:9:2]

+ omitting indices

my_list = range(1, 11) # List of numbers 1 - 10

# Add your code below!

# the default starting index is 0
# the default ending index is the end of the list
# the default stride is 1
print my_list[::2]

+reversing a list

my_list = range(1, 11)

# Add your code below!

# negative stride progresses
# through the list from right to left
backwards = my_list[::-1]

print backwards

+stride length

to_one_hundred = range(101)
# Add your code below!

backwards_by_tens = to_one_hundred[::-10]
print backwards_by_tens

+practise make perfect

to_21 = range(1,22)
odds = to_21[::2]
middle_third = to_21[7:14:]

print to_21
print odds
print middle_third

+anonymous functions

my_list = range(16)
print filter(lambda x: x % 3 == 0, my_list)

+ lambda syntax

languages = ["HTML", "JavaScript", "Python", "Ruby"]
# only "python" is returned by the filter
print filter(lambda x: x is 'Python' , languages)

+ lambda squares

squares = [x**2 for x in range(1, 11)]

print squares
print
print filter(lambda x: x >= 30 and x <=70, squares)

+ iterating over dictionaries

movies = {
	"Monty Python and the Holy Grail": "Great",
	"Monty Python's Life of Brian": "Good",
	"Monty Python's Meaning of Life": "Okay"
}

print movies.items()

+ comprehending comprehensions

# divisiable by 3 or 5
threes_and_fives = [x for x in range(1,16) if x%3 == 0 or x%5 == 0]

print threes_and_fives

+ list slicing

</pre>
garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"

# our message is backwards
backwards_garbled = garbled[::-1]

# the letter we want is every othre letter
message = backwards_garbled[::2]

print message
<pre>

+ lambda expressions

</pre>
garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

# filter out the 'X'
message = filter(lambda x: x is not 'X' ,garbled)
print message
<pre>

Viewing all articles
Browse latest Browse all 44

Trending Articles