Programming by Python - Lecture 2
26 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the function of triple quotes in Python?

  • To span a string across multiple lines. (correct)
  • To create a string variable only.
  • To represent comments in the code.
  • To escape characters in a string.
  • How can you assign multiple variables in one line in Python?

  • Using an array to hold the values.
  • Python does not support multiple assignments.
  • Using the syntax x = y = z = value.
  • Using combined variable assignments with commas. (correct)
  • What would be the result of the following code? x = 100; del x; print(x)

  • Error: variable x is deleted. (correct)
  • The value is stored but not printed.
  • None
  • 100
  • Which of the following statements is correctly utilizing single and double quotes in Python?

    <p>&quot;He said, 'Hello' to her.&quot;</p> Signup and view all the answers

    What does the print command do in Python?

    <p>It displays the values of variables or strings on the screen.</p> Signup and view all the answers

    What will be the output of the following code: a = int(20.5)?

    <p>20</p> Signup and view all the answers

    What is the result of converting the boolean value True using int()?

    <p>1</p> Signup and view all the answers

    What will happen if you try to convert the string 'Hello World' to an integer using int()?

    <p>ValueError</p> Signup and view all the answers

    What will be the output of the following code: a = int('110011', 2)?

    <p>51</p> Signup and view all the answers

    What will be the output of the following code: a = float('9.99')?

    <p>9.99</p> Signup and view all the answers

    What is the correct way to define a tuple containing the elements 'a', 7, and 2.2?

    <p>tuple = ( 'a', 7 , 2.2 )</p> Signup and view all the answers

    Which statement about dictionaries is correct?

    <p>Dictionaries consist of key:value pairs.</p> Signup and view all the answers

    How does a set in Python differ from a list?

    <p>Sets cannot contain duplicate elements, while lists can.</p> Signup and view all the answers

    What will the expression 'bool(a == b)' evaluate to if a = 2 and b = 4?

    <p>False</p> Signup and view all the answers

    Which of the following statements about Boolean data type is false?

    <p>Non-empty collections are considered False.</p> Signup and view all the answers

    What type of conversion occurs when performing an arithmetic operation between an int and a float?

    <p>Implicit conversion</p> Signup and view all the answers

    Which of the following is a valid syntax for initializing a dictionary in Python?

    <p>dict = {}</p> Signup and view all the answers

    What is the output of 'print(bool(()))'?

    <p>False</p> Signup and view all the answers

    What is the result of the following code? x = 100; print(x); del x; print(x)

    <p>Traceback (most recent call last): NameError: name 'x' is not defined</p> Signup and view all the answers

    Which Python function is used to retrieve the data type of a variable?

    <p>type()</p> Signup and view all the answers

    What will be the output of print(type(10.10))?

    <p>&lt;class 'float'&gt;</p> Signup and view all the answers

    How are elements in a Python list defined?

    <p>Separated by commas and enclosed in square brackets</p> Signup and view all the answers

    What will the following code output? str = 'Hello World!'; print(str[2:5])

    <p>llo</p> Signup and view all the answers

    Which of the following statements is true about tuples?

    <p>Tuples can contain different data types.</p> Signup and view all the answers

    What is the purpose of the + operator when used with strings in Python?

    <p>It concatenates the strings.</p> Signup and view all the answers

    What is the output of print(['a', 786, 2.23] + [1, 'c'])?

    <p>['a', 786, 2.23, 1, 'c']</p> Signup and view all the answers

    Study Notes

    Programming by Python - Lecture 2

    • Topics covered in lecture 2 include Python variable creation and deletion, and data types and casting in Python.
    • Python accepts single, double, and triple quotes for string literals.
    • Single and double quotes are used for short strings.
    • Single quotes can contain double quotes without escaping.
    • Double quotes can contain single quotes without escaping.
    • Triple quotes span strings across multiple lines.

    Creating Python Variables

    • Python creates a variable automatically when you assign a value.
    • The '=' sign assigns values to variables.
    • Variable declaration: Variable name = Value
    • Multiple assignments are allowed.
    • Combined assignments are allowed, separating variable names on the left and values on the right with commas.

    Printing Python Variables

    • Use the print() command.
    • Example: print(x), print(y), print(name), print(x, y, name).

    Deleting Python Variables

    • The del statement deletes references to number objects.
    • Example: x = 100, print(x), del x, print(x).

    Type of Variable

    • Use type() to determine a variable's data type.
    • Example: x = "Zara", y = 10, z = 10.10, print(type(x)), print(type(y)), print(type(z))
      • Output: <class 'str'>, <class 'int'>, <class 'float'>.

    Python Data Types

    • Python data types include:
      • Number: Integer, Float, Boolean, Complex
      • Sequence: String, List, Tuple
      • Set
      • Dictionary

    Sequence Data Types - String

    • Strings cannot be modified, you can only concatenate and slice.
    • Use slicing ([ ] or [:]) to get substrings, starting from 0 (first character); last character using -1.
    • Concatenation (+) combines strings.
    • Repetition (*) repeats a string.
    • Examples:
      • str = 'Hello World!', print(str)
      • print(str[0]), print(str[2:5]), print(str[2:]), print(str * 2), print(str + "TEST")

    Sequence Data Types - List

    • Lists are mutable (changeable) and store ordered collections of data.
    • Lists use square brackets ([]).
    • Elements are separated by commas.
    • Elements can be of different types.
    • Examples:
      • list = ['a', 786, 2.23, 'b', 70.2], print(list), print(list[0]), print(list[1:3]), print(list[2:]), print(tinylist * 2), print(list + tinylist).

    Sequence Data Types - Tuple

    • Tuples are immutable, meaning you cannot change them after creation.
    • Tuples use parentheses ().
    • Elements are separated by commas.
    • Example: tuple = ('a', 7, 2.2, 'b')
      • Accessing elements works like a list: tuple[0], tuple[2]

    Dictionary Data Types

    • Dictionaries store key-value pairs.
    • Use curly braces {}.
    • Key and value are separated by a colon :
    • Keys must be immutable (string, number, tuple)
    • Example: dict = {}, dict['one'] = "This is noha", dict[2] = "This is Yusef", print(dict['one']), print(dict2), print(dict2.keys()), print(dict2.values()).

    Set Data Type

    • Sets are unordered collections of unique elements.
    • Use curly braces {}.
    • Elements are separated by commas
    • No repeated elements.
    • Example: student_id = {1, 2, 3, 4, 5}, print('Student ID:', student_id), vowels = {'a', 'e', 'i', 'o', 'u'}, print('Vowel Letters:', vowels), mixed_set = {'Hello', 1, -2, 'Bye'}, print('Set of mixed data types:', mixed_set).

    Boolean Data Type

    • Represents True or False.
    • bool() function evaluates expressions/values.
    • True is equivalent to 1, False is equivalent to 0.

    Data Types Casting

    • Implicit: Automatic type conversion, e.g., operating on integers and floats.
      • True is treated as 1, and False as 0.
    • Explicit: Manual type conversion using functions like int(), float(), str().
      • Examples include int("100"), float("9.99"), str(10).
      • Note potential errors with explicit casting, e.g. conversion of strings that aren't valid numeric values.
    • Special case conversions: Converting between binary, octal, and hexadecimal numbers to decimal.

    Sequence Type Casting

    • Convert strings and tuples to lists using list() function.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    This quiz covers key concepts from Lecture 2 on Python, focusing on variable creation, deletion, and handling data types and casting. It discusses the use of quotes for string literals and the syntax for printing variables. Assess your understanding of these foundational programming skills.

    More Like This

    Use Quizgecko on...
    Browser
    Browser