PI Module 3

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the output of the following snippet? x = 4 y = 1 a = x & y b = x | y c = ~x # tricky! d = x ^ 5 e = x >> 2 f = x << 2 print(a, b, c, d, e, f)

0 5 -5 1 1 16

what does an if-elif-else statement do?

Checks a condition to see if it is true and if it isn't it checks the next condition and so on until it reaches a true else if statement or reaches the else statement

What does an if/else statement do?

Checks if a condition is true if it is not it executes the else portion

What is the output of the following snippet? x = 1 y = 0 z = ((x == y) and (x == y)) or not(x == y) print(not(z))

False

What is the output of the following snippet? x = 5 y = 10 z = 8 print(x > y) print(y > z)

False True

What is the output of the following snippet? x, y, z = 5, 10, 8 print(x > z) print((y - 5) == x)

False True

List comprehension

Here's an example of a list comprehension - the code creates a five-element list filled with with the first five natural numbers raised to the power of 3: cubed = [num ** 3 for num in range(5)] print(cubed) # outputs: [0, 1, 8, 27, 64]

slicing

If you want to copy a list or part of the list, you can do it by performing slicing: colors = ['red', 'green', 'orange'] copy_whole_colors = colors[:] # copy the entire list copy_part_colors = colors[0:2] # copy part of the list

What happens when you run the following snippet? 1st = [] del 1st print(1st)

NameError: name '1st' is not defined

x = 10 if x > 5: # True if x == 6: # False print("nested: x == 6") elif x == 10: # True print("nested: x == 10") else: print("nested: else") else: print("else") What is this an example of?

Nested Conditional Statements

logical operators

Python supports the following logical operators: and → if both operands are true, the condition is true, e.g., (True and True) is True, or → if any of the operands are true, the condition is true, e.g., (True or False) is True, not → returns false if the result is true, and returns true if the result is false, e.g., not True is False.

What does an if statement do?

The if statements is used to check a condition and if the condition is true it is executed

What does the range() function do?

The range() function generates a sequence of numbers. It accepts integers and returns range objects. The syntax of range() looks as follows: range(start, stop, step), where: start is an optional parameter specifying the starting number of the sequence (0 by default) stop is an optional parameter specifying the end of the sequence generated (it is not included), and step is an optional parameter specifying the difference between the numbers in the sequence (1 by default.) Example code: for i in range(3): print(i, end=" ") # Outputs: 0 1 2 for i in range(6, 1, -2): print(i, end=" ") # Outputs: 6, 4, 2

parameters are option

The start and end parameters are optional when performing a slice: list[start:end], e.g.: my_list = [1, 2, 3, 4, 5] slice_one = my_list[2: ] slice_two = my_list[ :2] slice_three = my_list[-2: ] print(slice_one) # outputs: [3, 4, 5] print(slice_two) # outputs: [1, 2] print(slice_three) # outputs: [4, 5]

What is the output of the following snippet? x, y, z = 5, 10, 8 x, y, z = z, y, x print(x > z) print((y - 5) == x)

True False

What is the output of the following snippet? x = 10 if x == 10: print(x == 10) if x > 5: print(x > 5) if x < 10: print(x < 10) else: print("else")

True True else

>=

True if the left operand's value is greater than or equal to the right operand's value, and False otherwise

>

True if the left operand's value is greater than the right operand's value, and False otherwise

<=

True if the left operand's value is less than or equal to the right operand's value, and False otherwise

<

True if the left operand's value is less than the right operand's value, and False otherwise

delete slices

You can delete slices using the del instruction: my_list = [1, 2, 3, 4, 5] del my_list[0:2] print(my_list) # outputs: [3, 4, 5] del my_list[:] print(my_list) # deletes the list content, outputs: []

exist in a list or no

You can test if some items exist in a list or not using the keywords in and not in, e.g.: my_list = ["A", "B", 1, 2] print("A" in my_list) # outputs: True print("C" not in my_list) # outputs: True print(2 not in my_list) # outputs: False

bitwise operators

You can use bitwise operators to manipulate single bits of data. The following sample data: x = 15, which is 0000 1111 in binary, y = 16, which is 0001 0000 in binary.

negative indices

You can use negative indices to perform slices, too. For example: sample_list = ["A", "B", "C", "D", "E"] new_list = sample_list[2:-1] print(new_list) # outputs: ['C', 'D']

nested list

You can use nested lists in Python to create matrices (i.e., two-dimensional lists). For example: # A four-column/four-row table - a two dimensional array (4x4) table = [[":(", ":)", ":(", ":)"], [":)", ":(", ":)", ":)"], [":(", ":)", ":)", ":("], [":)", ":)", ":)", ":("]] print(table) print(table[0][0]) # outputs: ':(' print(table[0][3]) # outputs: ':)'

What does the break statement do?

You use break to exit a loop, e.g text = "OpenEDG Python Institute" for letter in text: if letter == "P": break print(letter, end="")

What does the continue statement do?

You use continue to skip the current iteration, and continue with the next iteration, e.g.: text = "pyxpyxpyx" for letter in text: if letter == "x": continue print(letter, end="")

What is the output of the following snippet? a = "A" b = "B" c = "C" d = " " lst = [a, b, c, d] lst.reverse() print(lst)

[' ', 'C', 'B', 'A']

What is the output of the following snippet? list_1 = ["A", "B", "C"] list_2 = list_1[:] list_3 = list_2[:] del list_1[0] del list_2[0] print(list_3)

['A', 'B', 'C']

What is the output of the following snippet? lst = ["D", "F", "A", "Z"] lst.sort() print(lst)

['A', 'D', 'F', 'Z']

What is the output of the following snippet? list_1 = ["A", "B", "C"] list_2 = list_1 list_3 = list_2 del list_1[0] del list_2 print(list_3)

['B', 'C']

What is the output of the following snippet? list_1 = ["A", "B", "C"] list_2 = list_1 list_3 = list_2 del list_1[0] del list_2[0] print(list_3)

['C']

what is the output lst = [5, 3, 1, 2, 4] print(lst) lst.sort() print(lst)

[1, 2, 3, 4, 5]

What is the output of the following snippet? a = 3 b = 1 c = 2 lst = [a, c, b] lst.sort() print(lst)

[1, 2, 3]

What is the output of the following snippet? 1st = [1, 2, 3, 4, 5] lst_2 = [] add = 0 for number in 1st: add += number lst_2.append(add) print(lst_2)

[1, 3, 6, 10, 15]

What is the output of the following snippet? 1st = [1, [2, 3], 4] print(1st[1]) print(len(1st))

[2, 3] 3

What is the output of the following snippet? lst = [5, 3, 1, 2, 4] print(lst) lst.reverse() print(lst)

[4, 2, 1, 3, 5]

What is the output of the following snippet? lst = [1, 2, 3, 4, 5] lst.insert(1, 6) del lst[0] lst.append(1) print(lst)

[6, 2, 3, 4, 5, 1]

What is the output of the following snippet? list_1 = ["A", "B", "C"] list_2 = list_1 list_3 = list_2 del list_1[0] del list_2[:] print(list_3)

[]

What is the output of the following snippet? x = "1" if x == 1: print("one") elif x == "1": if int(x) > 1: print("two") elif int(x) < 1: print("three") else: print("four") if int(x) == 1: print("five") else: print("six")

four five

What does a while loop do?

loop executes a statement or a set of statements as long as a specified boolean condition is true

expected result. my_list = [1, 2, "in", True, "ABC"] print(1 ??? my_list) # outputs True print("A" ??? my_list) # outputs True print(3 ??? my_list) # outputs True print(False ??? my_list) # outputs False

my_list = [1, 2, "in", True, "ABC"] print(1 in my_list) # outputs True print("A" not in my_list) # outputs True print(3 not in my_list) # outputs True print(False in my_list) # outputs False

What is the output of the following snippet? x = 1 y = 1.0 z = "1" if x == y: print("one") if y == int(z): print("two") elif x == y: print("three") else: print("four")

one two

!=

returns True if operands' values are not equal, and False otherwise

==

returns if operands' values are equal, and False otherwise

What does the for loop do?

the for loop executes a set of statements many times; it's used to iterate over a sequence (e.g., a list, a dictionary, a tuple, or a set - you will learn about them soon) or other objects that are iterable (e.g., strings). You can use the for loop to iterate over a sequence of numbers using the built-in range function

meaning of bitwise operators in Python

will be used to illustrate the meaning of bitwise operators in Python. Analyze the examples below: & does a bitwise and, e.g., x & y = 0, which is 0000 0000 in binary, | does a bitwise or, e.g., x | y = 31, which is 0001 1111 in binary, ˜ does a bitwise not, e.g., ˜ x = 240*, which is 1111 0000 in binary, ^ does a bitwise xor, e.g., x ^ y = 31, which is 0001 1111 in binary, >> does a bitwise right shift, e.g., y >> 1 = 8, which is 0000 1000 in binary, << does a bitwise left shift, e.g., y << 3 = , which is 1000 0000 in binary,


संबंधित स्टडी सेट्स

ICEV - Animal Science Certification

View Set

Nurs 200 - Quizzes 3-8 / Prep For Final

View Set

Adaptive Quizzing Chapter 45 Nutrition

View Set

Quiz 14 Corporate Strategy - Vertical Integration

View Set

Pre-Ap World Geography Exam Review

View Set

UO BA 101 Midterm 3 Practice Problems

View Set