CS代考 Python_basics

Python_basics

Python Basics¶

Copyright By PowCoder代写 加微信 powcoder

3 + 2 + 1 – 5 + 4 % 2 – 1 / 4 + 6

st1 = ‘Anything “yes”‘

print(st1)

Anything “yes”

Tuples and Lists¶

tuple_months = (‘January’,’February’,’March’,’April’,’May’,’June’,\
‘July’,’August’,’September’,’October’,’November’,’December’)

tuple_months

(‘January’,
‘February’,
‘September’,
‘October’,
‘November’,
‘December’)

list_cats = [‘Tom’, ‘Snappy’, ‘Kitty’, ‘Jessie’, ‘Chester’]

[‘Tom’, ‘Snappy’, ‘Kitty’, ‘Jessie’, ‘Chester’]

print(list_cats[2])

list_cats.append(‘Catherine’)

[‘Tom’, ‘Snappy’, ‘Kitty’, ‘Jessie’, ‘Chester’, ‘Catherine’]

del list_cats[1]

[‘Tom’, ‘Kitty’, ‘Jessie’, ‘Chester’, ‘Catherine’]

list_cats[1:-2]

[‘Kitty’, ‘Jessie’]

list_cats[:-2]

[‘Tom’, ‘Kitty’, ‘Jessie’]

my_set = {1, 2, 3}

your_set = {4, 2, 5}

my_set | your_set

{1, 2, 3, 4, 5}

my_set – your_set

my_set & your_set

Dictionaries¶

CO2_by_year = {1799:1, 1800:70, 1801:74, 1802:82, 1902:215630, 2002:1733297}

# Look up the emissions for the given year
CO2_by_year[1801]

# Add another year to the dictionary
CO2_by_year[1950] = 734914

CO2_by_year

1902: 215630,
2002: 1733297,
1950: 734914}

CO2_by_year[2009] = 1000000
CO2_by_year[2000] = 100000

import numpy as np
CO2_by_year[2012] = np.nan

CO2_by_year

1902: 215630,
2002: 1733297,
1950: 734914,
2009: 1000000,
2000: 100000,
2012: nan}

1950 in CO2_by_year

1951 in CO2_by_year

len(CO2_by_year)

del CO2_by_year[1950]

len(CO2_by_year)

for key in CO2_by_year:
print(key)

for k in CO2_by_year.keys():

for v in CO2_by_year.values():

CO2_by_year.values()

dict_values([1, 70, 74, 82, 215630, 1733297, 1000000, 100000, nan])

for key, value in CO2_by_year.items():
print(key, value)

1902 215630
2002 1733297
2009 1000000
2000 100000

Control flow statements¶

range(0, 5)

list(range(5))

[0, 1, 2, 3, 4]

for n in range(5):
v.append(n**2)

[0, 1, 4, 9, 16]

Conditional statements

if val > 10:
print(“Large value”)
elif val >= 0:
print(“Small value”)
print(“Negative value”)

Small value

Functions¶

def convert_to_celsius(fahrenheit):
”’ (number) -> number
Return the celsius degrees equivalent to
fahrenheit degrees.
celsius = (fahrenheit – 32) * 5 / 9
return celsius

convert_to_celsius?

convert_to_celsius(32)

convert_to_celsius(212)

convert_to_celsius(-40)

def convert_to_kelvin(fahrenheit):
”’ (number) -> number
Return the number of kelvin degrees equivalent
to fahrenheit degrees.
kelvin = convert_to_celsius(fahrenheit) + 273.15
return kelvin

convert_to_kelvin(32)

import fibo

import fibo as fb

ar = fibo.fib2(4)

[1, 1, 2, 3]

List Comprehension¶

$S = \{x^2 : x ~ \mbox{in} ~ \{0 … 9\}\}$

range(0, 10)

list(range(10))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

S = [x**2 for x in range(10)]

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

list(range(10))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

$V = (1, 2, 4, 8, \ldots , 2^{12})$

V = [2**i for i in range(13)]

[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]

$M = \{ x ~ | ~ x ~ {\rm in} ~ S ~ {\rm and} ~ x ~ {\rm even} \}$

M = [x for x in S if x % 2 == 0]

[0, 4, 16, 36, 64]

noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
primes = [x for x in range(2, 50) if x not in noprimes]
print(primes)

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

words = ‘The quick brown fox jumps over the lazy dog’.split()
print(words)

[‘The’, ‘quick’, ‘brown’, ‘fox’, ‘jumps’, ‘over’, ‘the’, ‘lazy’, ‘dog’]

stuff = [[w.upper(), w.lower(), len(w)] for w in words]

[[‘THE’, ‘the’, 3],
[‘QUICK’, ‘quick’, 5],
[‘BROWN’, ‘brown’, 5],
[‘FOX’, ‘fox’, 3],
[‘JUMPS’, ‘jumps’, 5],
[‘OVER’, ‘over’, 4],
[‘THE’, ‘the’, 3],
[‘LAZY’, ‘lazy’, 4],
[‘DOG’, ‘dog’, 3]]

for i in stuff:

[‘THE’, ‘the’, 3]
[‘QUICK’, ‘quick’, 5]
[‘BROWN’, ‘brown’, 5]
[‘FOX’, ‘fox’, 3]
[‘JUMPS’, ‘jumps’, 5]
[‘OVER’, ‘over’, 4]
[‘THE’, ‘the’, 3]
[‘LAZY’, ‘lazy’, 4]
[‘DOG’, ‘dog’, 3]

stuff = map(lambda w: [w.upper(), w.lower(), len(w)], words)
for i in stuff:

[‘THE’, ‘the’, 3]
[‘QUICK’, ‘quick’, 5]
[‘BROWN’, ‘brown’, 5]
[‘FOX’, ‘fox’, 3]
[‘JUMPS’, ‘jumps’, 5]
[‘OVER’, ‘over’, 4]
[‘THE’, ‘the’, 3]
[‘LAZY’, ‘lazy’, 4]
[‘DOG’, ‘dog’, 3]

def ls_comp(ww):
return ([[w.upper(), w.lower(), len(w)] for w in ww])
stuff1 = ls_comp(words)

[[‘THE’, ‘the’, 3],
[‘QUICK’, ‘quick’, 5],
[‘BROWN’, ‘brown’, 5],
[‘FOX’, ‘fox’, 3],
[‘JUMPS’, ‘jumps’, 5],
[‘OVER’, ‘over’, 4],
[‘THE’, ‘the’, 3],
[‘LAZY’, ‘lazy’, 4],
[‘DOG’, ‘dog’, 3]]

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com