Data Types & Variable

Python built-in data types.

Data Types

In Python, data types are classifications of data items or values that determine the possible operations on them and the way they are stored in memory. Each programming language supports various data types, and these types define the type of data a variable can hold.

Several built-in data types:

Numeric Types:

Data Types Meanings Examples
int Integer numbers 5, -3, 100
float Floating-point numbers 3.14, -0.001, 2.0
complex Complex numbers 2 + 3j, -1 - 2j

Sequence Types:

Data Types Meanings Examples
list Ordered collections ['apple', 'banana', 'cherry']
tuple Immutable collections ('a', 'b', 'c')

Text Sequence Type:

Data Types Meanings Examples
str Represents Unicode strings 'hello', "world"

Mapping Type:

Data Types Meanings Examples
dict Collection of key-value pairs {'name': 'John', 'age': 30}

Set Types:

Data Types Meanings Examples
set Unordered collection of unique items {1, 2, 3}
frozenset Immutable set frozenset([1, 2, 3])

Binary Types:

Data Types Meanings Examples
bytes Represents a sequence of bytes b'hello'
bytearray Mutable version of bytes
memoryview Provides a view on memory for other binary data types.

Boolean Types:

Data Types Meanings Examples
bool Represents boolean values True or False 1, 0

Variables

Generally variables are used to store one type of data values, but in Python, you don't need to declare the type of a variable explicitly; Python have ability to automatically determines the type based on the value assigned to it. You can create a variable and assign a value to it using the assignment operator ('=').

Variable names must follow certain rules:

  1. Must start with a letter (a-z, A-Z) or underscore (_).
  2. Can be followed by letters, underscores, or digits (0-9).
  3. Variable names are case-sensitive.

Declaring variables:

py Copy Code
# Assigning values to variables
x = 5  # integer
y = 3.14  # float
name = "Ayan"  # string
is_student = True  # boolean

# You can also assign multiple variables in a single line
a, b, c = 1, 2.5, "techbaz.org"

# Output the values of variables
print(x, y, name, is_student)  # Output: 5 3.14 Ayan True
print(a, b, c)  # Output: 1 2.5 techbaz
Output:
5 3.14 Ayan True
1 2.5 techbaz.org

Variables with different types of values:

py Copy Code
x = 5       # integer
print(x)    # Output: 5

x = 'Ayan' # string
print(x)    # Output: Ayan
Output:
5
Ayan

Understanding data types and variables is crucial for effectively working with data and building programs in Python.

Format Specifiers

Format specifiers allow you to control how values are formatted within strings. Here's a quick overview of some common format specifiers:

Format Specifier Description
%s Formats the value as a string.
%r Similar to %s but uses repr() for non-string values.
%d Formats the value as an integer.
%x Formats the value as a lowercase hexadecimal integer.
%X Formats the value as an uppercase hexadecimal integer.
%o Formats the value as an octal integer.
%f Formats the value as a floating-point number.
%e Formats the value in scientific notation (lowercase).
%E Formats the value in scientific notation (uppercase).
%g Chooses between %f and %e based on the value.

You can use width and precision specifiers:

%10s Specifies a minimum width of 10 characters for the string.

%.2f Specifies precision for floating-point numbers (e.g., 2 decimal places).

These specifiers can be combined to achieve desired formatting for different types of data.

py Copy Code
# String Formatting
name = "Ayan"
print("Hello, %s!" % name)  # Output: Hello, Ayan!

# Integer Formatting
age = 28
print("I am %d years old." % age)  # Output: I am 28 years old.

# Floating Point Formatting
pi = 3.14159
print("The value of pi is approximately %f." % pi)
# Output: The value of pi is approximately 3.141590.

# Width and Precision Specifiers
name = "Sanvi"
print("Hello, %-10s!" % name)  # Output: Hello, Sanvi!

# Combining width and precision specifiers
price = 29.95
print("The price is $%7.2f." % price)  # Output: The price is $  29.95.
Output:
Hello, Ayan!
I am 28 years old.
The value of pi is approximately 3.141590.
Hello, Sanvi     !
The price is $  29.95.

You can experiment with different values and format specifiers to see how they affect the output.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.