





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Python Data Science Toolbox II. For loop and list comprehension syntax. In [1]: new_nums = [num + 1 for num in nums]. In [2]: for num in nums:.
Typology: Schemes and Mind Maps
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















In [1]: nums = [ 12 , 8 , 21 , 3 , 16 ] In [2]: new_nums = [] In [3]: for num in nums: ...: new_nums.append(num + 1 ) In [4]: print(new_nums) [13, 9, 22, 4, 17]
In [1]: new_nums = [num + 1 for num in nums] In [2]: for num in nums: ...: new_nums.append(num + 1 ) In [3]: print(new_nums) [13, 9, 22, 4, 17]
In [1]: result = [num for num in range( 11 )] In [2]: print(result) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [1]: pairs_1 = [] In [2]: for num1 in range( 0 , 2 ): ...: for num2 in range( 6 , 8 ): ...: pairs_1.append(num1, num2) In [3]: print(pairs_1) [(0, 6), (0, 7), (1, 6), (1, 7)]
In [1]: pairs_2 = [(num1, num2) for num1 in range( 0 , 2 ) for num in range( 6 , 8 )] In [2]: print(pairs_2) [(0, 6), (0, 7), (1, 6), (1, 7)]
In [1]: [num ** 2 for num in range( 10 ) if num % 2 == 0 ] Out[1]: [0, 4, 16, 36, 64]
In [1]: 5 % 2 Out[1]: 1 In [2]: 6 % 2 Out[2]: 0
In [1]: pos_neg = {num: -num for num in range( 9 )} In [2]: print(pos_neg) {0: 0, 1: -1, 2: -2, 3: -3, 4: -4, 5: -5, 6: -6, 7: -7, 8: -8} In [3]: print(type(pos_neg)) <class 'dict'>
In [1]: [ 2 * num for num in range( 10 )] Out[1]: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] In [2]: ( 2 * num for num in range( 10 )) Out[2]: <generator object
In [1]: result = (num for num in range( 6 )) In [2]: print(next(result)) 0 In [3]: print(next(result)) 1 In [4]: print(next(result)) 2 In [5]: print(next(result)) 3 In [6]: print(next(result)) 4