Python Coding Day 3 | Data Types: Strings, Integers, and Floats

💻 Data Types: Strings, Integers, and Floats

Watch the lesson tutorial  🔻



In programming, a data type is a classification that tells the compiler or interpreter how the programmer intends to use the data. It essentially defines the type of value a variable can hold and the operations that can be performed on it.

1. String (str) 📝

A string is a sequence of characters. It is used to store text. In most languages, strings are enclosed in either single quotes (') or double quotes (").

  • What it stores: Text, words, sentences, symbols, and even numbers (when treated as text).

  • Key Feature: Strings are typically immutable, meaning once they are created, they cannot be changed—any operation that appears to "change" a string actually creates a new string.

Practical Example (Python Syntax)

Python
# Storing a name
blog_title = "The University Coder's Guide"

# Storing a complete sentence
post_content = 'Today, we dive into data types.'

# Example of string operations (concatenation and length)
author = "Gemini"
full_line = blog_title + " by " + author
print(full_line)  # Output: The University Coder's Guide by Gemini

# Finding the length of a string
print(len(full_line)) # Output: 40 (counts all characters, including spaces)

2. Integer (int) 🔢

An integer is a whole number (positive, negative, or zero) without any fractional or decimal part.

  • What it stores: Counts, quantities, indices, and any value that must be an exact whole number.

  • Key Feature: Modern programming languages often use arbitrary-precision integers, meaning the size of the number is limited only by the computer's memory.

Practical Example (Python Syntax)

Python
# Storing a student's ID number or a course code
student_id = 100345678

# Storing the number of articles published
published_articles = 15

# Example of integer operations (arithmetic)
days_in_week = 7
total_weeks = 4
total_days = days_in_week * total_weeks
print(total_days) # Output: 28

# Checking if a number is an integer
print(isinstance(published_articles, int)) # Output: True

3. Floating-Point Number (float) 🔬

A floating-point number (or float) is a number that has a decimal point and is used to represent real numbers. The term "floating-point" refers to the way the decimal point "floats" to accommodate a wide range of values.

  • What it stores: Measurements, calculations involving division, currency (though often a special Decimal type is preferred), and any value requiring a fractional part.

  • Key Consideration: Due to how computers store floats in binary, they can sometimes lead to minor precision errors (e.g., $0.1 + 0.2$ might not exactly equal $0.3$).

Practical Example (Python Syntax)

Python
# Storing a student's GPA
gpa = 3.85

# Storing a measured temperature
temperature = 98.6

# Example of float operations (division and subtraction)
price_of_laptop = 1299.99
sale_discount = 150.50
final_price = price_of_laptop - sale_discount
print(final_price) # Output: 1149.49

# Showing the precision issue (common in all languages)
test_math = 0.1 + 0.2
print(test_math) # Output: 0.30000000000000004 (not exactly 0.3!)

💡 Quick Comparison Table

Data TypeRepresentsExample ValueEnclosureUsage
stringTextual data"Hello World"Quotes (", ')Names, messages, blog content
intWhole numbers42NoneCounts, ages, loop counters
floatNumbers with decimals3.14159NoneMeasurements, financial calculations

- by Chirana Nimnaka


Comments

Popular posts from this blog

Python Coding Day 1 | The print() Function and Comments

What is Python?

Set Up an Environment to Code in Python