Python

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Which character is used to define a Python comment?

#

What will be the result of the following syntax: mylist = ['apple', 'banana', 'cherry']print(mylist[-1])

Cherry

What is Boolean value in Python

Is a type of data that only has two possible values True or False. You think of it as a yes or no question. Yes = True , No = False

The statement below would print a Boolean value, which one? print(bool("abc"))

True

The statement below would print a Boolean value, which one? print(10 > 9)

True

True or False: You can declare string variables with single or double quotes in Python ex: x = "John" # is the same as x = 'John'

True

What will be the result of the following syntax: print(5 > 3)?

True

In Python, string indexing is _____ -based

Zero

What will be the result of the following syntax: mylist = ['apple', 'banana', 'cherry', 'orange', 'kiwi'] print(mylist[1:4])

['banana', 'cherry', 'orange']

Insert the correct syntax to convert x into a integer. x = 5.5 x = ___(x)

int

Return the string without any whitespace at the beginning or the end. txt = " Hello World " x =________

x = txt.strip()

What will be the result of the following syntax: x = 5 x += 3 print(x)

8

What does list mean in Python?

a data structure in Python that is mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Lists are defined by having values between square brackets [ ].

What will be the result of the follow syntax: mylist = ['apple', 'banana', 'cherry'] print(mylist[1])

banana

Insert the missing part of the code below to output "Hello World" in Python. _____("Hello World")

print

Complete the code block, print "YES" if 5 is larger than 2 in Python. if 5 > 2: __________

print("YES")

The following Python code example would print the data type of x, what data type would that be? x = "Hello World" print(type(x))

str

What is the correct file extension for Python files?

.py

The statement below would print a Boolean value, which one? print(10 < 9)

False

Consider the following code in Python: print('Hello', 'World') what will be the print result?

Hello World

Consider the following code in Python: a = 'Hello' b = 'World' print (a + b) What will be the printed result?

HelloWorld

Consider the python code: x = 'awesome' def myfunc(): global x x = 'fantastic' myfunc() print('Python is ' + x) What will be the printed results?

Python is Fantastic

Use a multiline string to make the multiline comment in Python: ___ This is a comment written in more than just one line ___

""" """

Comments in Python are written with a special character, which one? __This is a comment

#

What is the difference between tuple and lists in Python?

- Tuples are immutable objects and lists are mutable objects. - Once defined, tuples have a fixed length and lists have a dynamic length. - Tuples use less memory and are faster to access than to lists. - Tuple syntax uses round brackets or parenthesis, and list syntax uses square brackets.

What will be the result of the following code: print(int(35.88))

35

What will be the result of the following code: print(str(35.82))

35.82

Consider the following code in Python: a = 4 b = 5 print(a + b) What would be the output?

9

What is len() in Python?

Calculates the length of objects like strings, lists, and dictionaries. Understanding this essential built-in function helps developers write more efficient code and handle data structures effectively.

What does != mean in Python?

Does not equal sign. If two values are indeed not equal to each other, the return value is true.

The statement below would print a Boolean value, which one? print(10 == 9)

False

The statement below would print a Boolean value, which one? print(bool(0))

False

True or False: List items cannot be removed after the list has been created.

False

True or False: Indentation in Python is for readability only.

False

What is mutable in Python?

If its state or value can be modified after it is created. This means that you can alter its internal data or attributes without creating a new object.

What is the correct syntax to print a string in upper case letters? a. 'Welcome'.upper() b. 'Welcome'.toUpper() c. 'Welcome'.toUpperCase()

a. 'Welcome'.upper()

Which is NOT a legal variable name in Python? a. my-var = 20 b. my_var = 20 c. Myvar = 20 d. _myvar = 20

a. my-var = 20

if x = 9, what is a correct syntax to print 'The price is 9.00 dollars'? a. print(f'The price is {x:.2f} dollars') b. print(f'The price is {x:.2} dollars') c. print(f'The price is {x:.format(2)} dollars')

a. print(f'The price is {x:.2f} dollars')

What is string slicing in python

allows you to extract a portion of a string by specifying a start and end index. It follows the format string [start:end], where start is the index where slicing begins (inclusive) and end is the index where it ends (exclusive).

The following python code example would print the data type of x, what data type would that be? x = ["apple", "banana", "cherry"] print(type(x))

list

What is tuple in Python?

Is a collection used to store multiple items in a single variable. It has two main characteristic order and immutable. It is in a defined order and you cannot change, add, or remove items.

What is the or operator used for in python?

Is a logical operator in Python that return True if at least one of its conditions is true. It is commonly used in if statements to check multiple possibilities.

What is an example of immutable in Python?

Numbers (Int, Float, and complex numbers), strings, and tuples

Consider the following python code: x = 'awesome' def myfunc(): x = 'fantastic' myfunc() print('Python is ' + x) What will be the printed results?

Python is awesome

What is bool in python?

The data type is used to store two values i.e True and False. This is used to test whether the result of an expression is true or false. They are crucial for conditional statements like if, else, and while loops.

What will be the result of the following code: print(f'The price is {2 + 3} dollars')

The price is 5 dollars

What is int in Python

This refers to the integer data type. Integers are whole numbers that can be positive, negative, or zero.

What is a f-string in python?

an f-string is a literal string, prefixed with 'f', which contains expressions inside braces. The expressions are replaced with their values. So if you use f before any string part in curly bracket is replaced by a value, formal, etc. If you use tripple quotes it denotes multiline text.

Consider the following code in Python: fruits = ['apple, 'banana', 'cherry'] a, b, c = fruits print(a) What will be the result of a?

apple

What is NOT a legal numeric data type in Python? a. int b. long c. float

b. long

What will be the result of the following syntax: mylist = ['apple', 'banana', 'banana', 'cherry'] print(mylist[2])

banana

The following Pythoncode example would print the data type of x, what data type would that be? x = True print(type(x))

bool

What will be the result of the following code: x = 'Welcome' print (x[3])

c

What is a correct syntax to merge variable x and y into variable z? a. z = x, y b. z = x = y c. z = x + y

c. z = x + y

Create a variable named carname and assign the value Volvo to it in Python ____ = ____

carname = "Volvo"

what does the in command do in Python code?

checks if a specified value exists within a sequence, such as a list, tuple, or string.

What will be the result of the following code: x = 'Welcome' print(x[3:5])

co

What will be the result of the following code? x = 'welcome' print(x[3:])

come

Insert the correct syntax to convert x into a complex number. x = 5 x = ___(x)

complex

Select the correct functions to print the data type of a variable in Python: _____(____(myvar)) a. typ b. type c. var d. print e. echo

d. print b. type print(type(myvar))

What is the correct way to declare a Python variable? a. var x=5 b. #x = 5 c. $x = 5 d. x = 5

d. x = 5

The following Python code example would print the data type of x, what data type would that be? x = {"name" : "John", "age" : 36} print(type(x))

dict

Insert the correct syntax to convert x into a floating point number. x = 5 x = ____(x)

float

The following Python code example would print the data type of x, what data type would that be? x = 20.5 print(type(x))

float

Insert the correct keyword to make the variable x belong to the global scope in python: def myfunc(): ____ x x = "fantastic"

global

Use the correct membership operator to check if "apple" is present in the fruits object. fruits = ["apple", "banana"] if "apple" __ fruits: print("Yes, apple is a fruit!")

if "apple" in fruits:

Use the correct logical operator to check if at least one of two statements is True. if 5 == 10 ___ 4 == 4: print("At least one of the statements is true")

if 5 == 10 or 4 == 4

Insert the correct keyword to check if the word 'free' is present in the text: txt = 'The best things in life are free!' if 'free' _____ txt: print ('Yes, free is present in the text.')

in

The following Python code example would print the data type of x, what data type would that be? x = 5 print(type(x))

int

What is dict in Python?

is Python's key/value hash table structure. The contents of this can be written as a series of key:value pairs within brackets { }. It is a goode idea to use them whenever you want to find (lookup for) a certain Python object. You can also use lists for this but they are much slower than dict.

What is str in Python?

is a built in data type representing a sequence of characters, commonly know as a string. Strings are used to store and manipulate textual data.

What is string strip() used for?

is used to remove leading and trailing characters from a string. By default, it removes whitespace characters such as spaces, tabs, and newlines.

Select the correct function for returning the number of items in a list: thislist = ['apple', 'banana', 'cherry'] print(_______(thislist))

len

What is the difference between immutable and mutable types in Python?

mutable types (like lists, dictionaries) can be changed after creation, while immutable types (like strings, tuples) cannot be altered once defined, ensuring data integrity and safety.

Multiply 10 with 5, and print the result. print(10 __ 5)

print(10 * 5)

Divide 10 by 2, and print the result. print(10 __ 2)

print(10/2)

Consider this code: a = 'Join' b = 'the' c = 'party' What is a correct syntax to print 'Join the party'?

print(a + ' ' + b + ' ' + c)

Print the second item in the fruits list. fruits = ["apple", "banana", "cherry"] print(_____)

print(fruits[1])

Use a range of indexes to print the third, fourth, and fifth item in the list. fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(fruits[ _____ ])

print(fruits[2:5])

In Python if x = 5, what is a correct syntax for printing the data type of the variable x?

print(type(x))

What is the correct command line syntax for checking if python is installed on your computer?

python --version

The following Python code example would print the data type of x, what data type would that be? x = ("apple", "banana", "cherry") print(type(x))

tuple

Insert the correct syntax to add a placeholder for the age parameter. age = 36 txt = f"My name is John, and I am ______" print(txt)

txt = f"My name is John, and I am {age}"

Replace the charcter H with a J. txt = "Hello World" txt = txt.____( ___, ____)

txt = txt.replace("H", "J")

Convert the value of txt to upper case. txt = "Hello World" txt = ______

txt = txt.upper()

Convert the value of txt to lower case. txt = "Hello World" txt = _______

txt.lower()

What is the correct syntax to add the value 'Hello World', to 3 (x, y, z) variables in one statement in Python?

x = y = z = 'Hello World'

Insert the correct syntax to assign values to multiple variables in one line in Python: x_ y_ z = "Orange", Banana", "Cherry"

x, y, z = "Orange", "Banana", "Cherry"

Use the len function to print the length of the string. x = "Hello World" print(____)

print(len(x))

What will be the result of the following code: print(float(35))

35.0

True or False: Variable names are not case-sensitive in Python ex: a = 5 # is the same as A = 5

False

What is examples of mutable objects in Python?

List, dict, and sets

What is float in Python?

Means it is a number that has a decimal place

What will be the result of the following code: x = 'Welcome' y = 'Coders' print(x + y)

WelcomeCoders

What is the correct syntax to exit the Python command line interface?

exit()

Use the correct comparison operator to check if 5 is not equal to 10. if 5 __ 10: print("5 and 10 is not equal")

if 5 != 10:

Create a variable named x and assign the value 50 to it in Python

x = 50

Get the first character of the string txt. txt = "Hello World" x = _____

x = txt[0]

Get the characters from index 2 to index 4 (llo). txt = "Hello World" x = ____

x = txt[2:5]


Ensembles d'études connexes

Chapter 23 The Evolution of Populations

View Set

Telecom exam 3, IS-3413 Final Exam, IS 3413 Test 3, Telecom Test 3 (Ch 9, 10, 11, 12), Test #3 Study Guide

View Set