In this lesson, I learned about data abstraction in computer science, which is useful for building basic code functions and defining different stuff.
Variables: Variables are like containers inside a program that can hold values. They act as storage units, helping us manage and organize data. Proper variable names are important. For instance, using a variable to store someone’s age:
age = 25
Data Types: We discovered different data types for storing variables. For example, integers (int) for numbers, strings for texts and words, and booleans for true/false values:
name = "Sri"
temperature = 75.5
is_raining = False
Naming Conventions: Naming variables correctly is vital. Variable names should be descriptive, short, and follow specific rules like starting with a letter, not using spaces, and avoiding dashes. For instance:
high_score = 87
num_students = 40
is_raining_today = True
Concatenation: We explored the idea of joining different types of data, like strings. For example, combining a first name and last name:
first_name = "Sri"
last_name = "Surapaneni"
full_name = first_name + " " + last_name
Formatting: We learned about formatting data to display it according to specific rules, making it more readable. For instance:
message = "my name is {0} and I am {1} years old."
print(message.format("Sri", 15))
Conversions: We saw how to convert data from one type to another, such as converting a dictionary into a string and back. For instance:
import json
my_dict = {"A": 1, "B": 2}
my_string_dict = json.dumps(my_dict)
my_dict_recreated = json.loads(my_string_dict)