Python Data Types

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which data type is used to store a sequence of characters in Python?

  • int
  • bool
  • float
  • str (correct)

In Python, a variable's data type is explicitly declared before assigning a value to it.

False (B)

Name three of the built-in numeric data types in Python.

int float complex

The __________ data type is used to store ordered collections of items in Python, which are mutable.

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

Match the following Python data types with their corresponding descriptions:

<p>int = Integer numbers float = Floating-point numbers complex = Complex numbers str = Textual data</p> Signup and view all the answers

Which of the following data types is immutable?

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

You can convert a complex number directly into an integer using the int() function in Python.

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

What is the primary difference between a set and a frozenset in Python?

<p>Sets are mutable, while frozensets are immutable.</p> Signup and view all the answers

The __________ data type represents boolean values, which can be either True or False.

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

Match the following Python code snippets with their corresponding data types:

<p><code>x = 5</code> = int <code>x = 3.14</code> = float <code>x = 'Hello'</code> = str <code>x = [1, 2, 3]</code> = list</p> Signup and view all the answers

Which of the following functions can be used to convert a number to a floating-point number?

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

Python has a built-in function called random() that generates a random number.

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

Explain the use of the range() function in Python.

<p>Used to generate a sequence of numbers</p> Signup and view all the answers

The __________ data type stores key-value pairs in Python.

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

Match each operator with its description:

<ul> <li>= Addition</li> </ul> <ul> <li>= Subtraction</li> </ul> <ul> <li>= Multiplication / = Division</li> </ul> Signup and view all the answers

What will the following code output?

x = 10
y = 3
print(x % y)

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

The ** operator in Python is used for multiplication.

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

What is the purpose of the // operator in Python?

<p>floor division</p> Signup and view all the answers

The __________ operator is used to assign a value to a variable.

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

Match the assignment operators with their equivalent expressions:

<p>x += 5 = x = x + 5 x -= 5 = x = x - 5 x *= 5 = x = x * 5 x /= 5 = x = x / 5</p> Signup and view all the answers

What will the following code output?

x = 5
y = 10
print(x > y)

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

The == operator checks if two values are not equal.

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

Name three comparison operators in Python.

<p>== != &gt; &lt; &gt;= &lt;=</p> Signup and view all the answers

The __________ operator returns True if both statements are true.

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

Match each logical operator with its description:

<p>and = Returns True if both statements are true or = Returns True if one of the statements is true not = Reverses the result, returns False if the result is true</p> Signup and view all the answers

What will the following code output?

x = True
y = False
print(x or y)

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

The not operator returns True if the statement is true.

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

Explain the purpose of logical operators in Python.

<p>Combine conditional statements</p> Signup and view all the answers

In Python, the ______ data type is used to store a sequence of characters.

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

Match data types with their example values:

<p>int = 42 float = 3.14 str = Hello bool = True</p> Signup and view all the answers

Flashcards

Data Type

A category defining the type of value a variable can hold, such as text, numbers, or booleans.

String (str)

A built-in Python data type representing a sequence of characters.

Integer (int)

A built-in Python data type representing whole numbers.

Float

A built-in Python data type representing numbers with a decimal point.

Signup and view all the flashcards

Complex

A built-in Python data type representing complex numbers (with a real and imaginary part).

Signup and view all the flashcards

List

A built-in Python data type representing an ordered sequence of items, which can be of different types.

Signup and view all the flashcards

Tuple

A built-in Python data type representing an ordered, immutable sequence of items.

Signup and view all the flashcards

Range

A built-in Python data type representing an immutable sequence of numbers.

Signup and view all the flashcards

Dictionary (dict)

A built-in Python data type representing a collection of key-value pairs.

Signup and view all the flashcards

Set

A built-in Python data type representing an unordered collection of unique items.

Signup and view all the flashcards

Boolean (bool)

A built-in Python data type representing one of two values: True or False.

Signup and view all the flashcards

Type Conversion

Functions that allow you to change the data type of a variable.

Signup and view all the flashcards

Random Module

A Python module used for generating random numbers.

Signup and view all the flashcards

Operators

Symbols used to perform operations on variables and values.

Signup and view all the flashcards

Arithmetic Operators

Operators used to perform mathematical calculations.

Signup and view all the flashcards

Assignment Operators

Operators used to assign values to variables.

Signup and view all the flashcards

Comparison Operators

Operators used to compare two values.

Signup and view all the flashcards

Logical Operators

Operators used to combine conditional statements.

Signup and view all the flashcards

Study Notes

  • In programming, data type is an important concept
  • Variables can store data of different types, and different types can perform different actions
  • Python has the following built-in data types by default:

Built-in Data Types

  • Text Type: str
  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range
  • Mapping Type: dict
  • Set Types: set, frozenset
  • Boolean Type: bool
  • Binary Types: bytes, bytearray, memoryview

Setting the Data Type

  • In Python, the data type is set when you assign a value to a variable
Example Data Type
x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
x = {"name": "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set

Python Numbers

  • There are three numeric types in Python: int, float, and complex
  • Variables of numeric types are created when a value is assigned to them

Int

  • Integers can be positive or negative, without decimals, and of unlimited length
  • For example:
x = 1
y = 35656222554887711
z = -3255522

print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'int'>
print(type(z)) # Output: <class 'int'>

Float

  • Floats are numbers containing a decimal point, or can be exponential numbers
  • For example:
x = 1.10
y = 1.0
z = -35.59

print(type(x)) # Output: <class 'float'>
print(type(y)) # Output: <class 'float'>
print(type(z)) # Output: <class 'float'>

Complex

  • Complex numbers are written with a "j" as the imaginary part
  • For example:
x = 3+5j
y = 5j
z = -5j

print(type(x)) # Output: <class 'complex'>
print(type(y)) # Output: <class 'complex'>
print(type(z)) # Output: <class 'complex'>

Type Conversion

  • Conversion from one type to another can be performed with the int(), float(), and complex() methods
  • Example:
x = 1 # int
y = 2.8 # float
z = 1j # complex

## convert from int to float:
a = float(x)
print(a) # Output: 1.0
print(type(a)) # Output: <class 'float'>

## convert from float to int:
b = int(y)
print(b) # Output: 2
print(type(b)) # Output: <class 'int'>

## convert from int to complex:
c = complex(x)
print(c) # Output: (1+0j)
print(type(c)) # Output: <class 'complex'>
  • Complex numbers cannot be converted into another number type

Random Number

  • To make a random number, Python utilizes a built-in module called random
  • Example:
import random

print(random.randrange(1, 10)) # Output: 5

Python Booleans

  • Booleans represent one of two values: True or False

Boolean Values

  • It's often necessary to determine whether an expression is True or False in programming
  • Evaluation of any expression in Python yields either True or False
  • Comparison of two values leads to Python evaluating the expression and returning the Boolean answer

Example

print(10 > 9) # Output: True
print(10 == 9) # Output: False
print(10 < 9) # Output: False
a = 200
b = 33

if b > a:
  print("b is greater than a")
else:
  print("b is not greater than a") # Output: b is not greater than a

Python Operators

  • Operators are used to perform operations on variables and values
  • Python divides the operators into the following groups:
    • Arithmetic operators
    • Assignment operators
    • Comparison operators
    • Logical operators
    • Identity operators
    • Membership operators
    • Bitwise operators

Arithmetic Operators

  • Arithmetic operators are used with numeric values to perform common mathematical operations
Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x**y

Example

x = 15
y = 4

print('x + y =',x+y) # Output: x + y = 19
print('x - y =',x-y) # Output: x - y = 11
print('x * y =',x*y) # Output: x * y = 60
print('x / y =',x/y) # Output: x / y = 3.75
print('x // y =',x//y) # Output: x // y = 3
print('x ** y =',x**y) # Output: x ** y = 50625

Assignment Operators

Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3

Example

a = 10
b = a
print(b) # Output: 10

b += a
print(b) # Output: 20

b -= a
print(b) # Output: 10

b *= a
print(b) # Output: 100

b <<= a
print(b) # Output: 102400

Comparison Operators

  • Comparison operators are used to compare two values
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Example

x = 10
y = 12

print('x > y is',x>y) # Output: x > y is False
print('x < y is',x<y) # Output: x < y is True
print('x == y is',x==y) # Output: x == y is False
print('x != y is',x!=y) # Output: x != y is True
print('x >= y is',x>=y) # Output: x >= y is False
print('x <= y is',x<=y) # Output: x <= y is True

Logical Operators

  • Logical operators are used to combine conditional statements
Operator Description Example
and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

Example

x = True
y = False

print('x and y is',x and y) # Output: x and y is False
print('x or y is',x or y) # Output: x or y is True
print('not x is',not x) # Output: not x is False

Studying That Suits You

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

Quiz Team

Related Documents

Use Quizgecko on...
Browser
Browser