# for for i in [ 1, 2, 3, 4, 5] : print (i) # for i for j in (1, 2, , 4, 5 ] : print ( j ) # for j print (i + j) # for j print (i) # for i print ( " ")
# long_winded_computation = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20)
# list_of_lists = [ [ 1 , 2, 3 ) , [4, 5, 6 ] , [ 7 , 8, 9 ] ] # easy_to_read_list_of_lists = [1, 2, 3 ) , [4, 5, 6 ) , [7, 8, 9 ) ]
two_plus_three = 2 + \ 3
for i in [ 1, 2, 3, 4, 5] : # print (1)
# : IndentationError : expected an indented blk
import re my_regex = re.compile ("[0-9]+",re.I)
import re as regex my_regex = regex.compile("[0-9)+",regex.I)
import matplotlib.pyplot as plt
from collections import defaultdict , Counter lookup = defaultdict(int) my_counter = Counter()
def double() : """, , docstring, , . , 2""" return * 2
# f def apply_to_one(f): '""' f """ return f(1) my _ double = double # = apply_to_one(my_double) # = 2
= apply_to_one(lambda : + 4) # = 5
another double = lmbd : 2 * # def another_double (x) : return 2 * #
def my_print (message="oe " ): print (message ) my_print ( "pe") # '' my_print () # ' '
# def subtract ( a=0, =0 ) : return - b subtract (10, 5)# 5 subtract (0, 5)# -5 subtract (b=5 )# ,
single_quoted_string = ' ' # double_quoted_string = " " #
tab_string = "\t" # len (tab_string)# = 1
not_tab_string = r"\t" # ' \ ' ' t ' len (not_tab_string) # = 2
multi_line_string = """ . """
try: print (0 / 0) except ZeroDivisionError : rint ( " ")
integer_list = [1, 2, ] # heterogeneous_list = ["", 0.1 , True] # list_of_lists = [integer_list, heterogeneous_list, [] ] # list_length = len(integer_list) # = 3 list_sum = sum(integer_list)# = 6
= list(range (10)) # {0, 1 , . . . , 9] zero = [0] # = 0 , -, . . 1- = 0 one = x [1] # = 1 nine = [-1] # = 9, - eight = [-2] # = 8, - [0] = -1 # = { - 1 , 1 , 2, 3, . . . , 9]
first_three = [:] # = [-1 , 1, 2] three_to_end = [3:] # = {3, 4, ... , 9] one_to_four = [1:5] # = {1 , 2, 3, 4] last_three = [-3:] # = { 7, 8, 9] without_first_and_last = x[1:-1] # = {1 , 2, ... , 8] _ of _ = [:] # = [ -1, 1, 2, ... , 91
1 ln [1, 2, 3] #True 0 ln [1, 2, 3] #False
= [1, 2, 3] . extend ( [ 4, 5, 6] ) # = {1, 2, 3, 4, 5, 6}
= [1, 2, 3] = + [4, 5, 6] #= (1, 2, 3, 4, 5, 6] ;
= [1, 2, 3] x.append (0)# = [1,2,3,0] = [-1] # = 0 z = len (x)# = 4
, = [1, 2] # = 1, = 2
_, = [1, 2] # == 2,
my_list = [1, 2] # my_tuple = (1, 2) # other_tuple = 3, 4 # my_list [1] = 3 # my_list = [1 , 3] try: my_tuple [1] = 3 except ypeError : print ( " " )
# def sum_and_product (x, ) : return ( + ) , ( * ) sp = sum_and_product (2, 3) # = (5, 6) s, = sum_and_product (S, 10) # s = 15, = 50
, = 1, 2 # = 1, = 2 , = , # -; = 2, = 1
empty_dict = {} # - empty_dict2 = dict () # - grades = { "Grigoriy" : 80, "Tim" : 95 } # ( )
rigory_aleksee = grades[ "Grigoriy"] # = 80
try: kates_grade = grades [ "Kate "] except eyError: rint ( " ! " )
grigoriy_has_grade = "Grigoriy" in grades #true kate_has_grade = "Kate" in grades #false
grigoriy_grade = grades. get ( "Grigoriy ", 0) # =80 kates_grade = grades.get ("Kate" , 0) # = 0 no_ones_grade = grades.get ( "No One" ) # = None
grades [ "Tim" ] = 99 # grades [ "Kate"] = 100 # num_students = len(grades) # = 3
tweet = { "user" : " grinaleks", "text" : " - ", " retweet_count" : 100, "hashtags " : [ "# data", " #science", " #datascience " , " #awesome", "#yolo" ] }
tweet_keys = tweet.keys() # tweet_values = tweet.values() # tweet_items = tweet.items() # (, ) "user" in tweet_keys # True, in "user" in tweet # -, in "grinaleks" in tweet_values # True
# word_ counts = { } document = { } # ; for word in document : if word in word counts: word_counts [word] += 1 else : word_counts [word] = 1
word_ counts = { } for word in document : try: word_counts [word] += 1 except eyError : word_counts [word] = 1
word_counts = { } for word in document : previous_count = word_counts.get (word, 0) word_counts [word] = previous_count + 1
from collections import defaultdict word_counts = defaultdict(int) # int () 0 for word in document : word_counts[word] += 1
dd_list = defaultdict (list)# list () dd_list [2].append (l) # dd_list (2: {1] } dd_dict = defaultdict (dict ) # dict () dict dd_dict ["Grigoriy"] [ "City" ] = "Seattle" # { "Grigoriy" : { "City" : Seattle"} dd_pair = defaultdict (lambda: [0,0] ) dd_pair [2][1] = 1 # dd_pair (2 : {0,1] }
from collections import Counter = Counter([0,1,2,0]) # = { 0 : 2, 1 : 1, 2 : 1 }
# word_counts = Counter (document)
# 10 () for word, count in word_counts.most_common(10) : print (word, count )
s = set ()# s.add (1) # s = { 1 } s.add (2) # s = { 1, 2 } s.add (2) # s = { 1, 2 } = len (s) # = 2 = 2 in s # = True z = 3 in s # = False
# - stopwords_list = [ "a", "an" , "at "] + hundreds_of_other_words + [ "yet ", " you"] " zip" in stopwords_list # False, # - stopwords_set = set(stopwords_list) " zip" in stopwords_set #
item_list = [1, 2, 3, 1, 2, 3] # num_items = len( item_list) # = 6 item_set = set(item_list) # (1, 2, 3} num_distinct_items = len(item_set) # = 3 distinct_item_list = list(item_set) # = [1,2,3]
if 1 > 2: message " 1 2 . . . " elif 1 > 3: message "elif 'else if '" else: message = " , else "
parity = "" if % 2 === else " "
= 0 while < 10: print (x, " 10") += 1
for in range (lO) : print (x, " 10" ) 51
continue break: for 1n range (10) : 1f == 3: continue # if == 5: break print (x) #
one_is_less_than_two = 1 < 2 #True true_equals_false = True == False #False
= None print (x == None )# True, - print ( is None ) # True -
s = some_function_that_returns_a_string () # if s: first_char = s [0] # else: first char = ""
first_char = s and s [0]
since the logical operator and returns the second value, if the first is true, and the first value, if it is false. Similarly, if x in the following expression is either a number, or perhaps None, then the result will somehow be a number: safe = or 0 #
all ( [True, 1, { 3 }]) # True all ( [True, 1, {}] ) # False, {} = any ( [ True, 1, {}]) # True, True = all ( [] ) # True, any ( [ ] ) # False,
Source: https://habr.com/ru/post/450474/
All Articles