Notes on Variables and Data Types in Programming
Variable Types in Programming
Strings
Defined as a series of characters.
Can use either double quotes or single quotes.
Preference for double quotes is stated.
Example:
To demonstrate:
first_namebehaves as if it holds the value of a string.Printing the variable:
Use without quotes to print the value:
print(first_name)prints the actual name rather than the string "first_name".
Formatted String Literals (f-strings)
Syntax:
f"<text> {variable}"The
findicates it is a formatted string.Example:
Text: 'hello', variable:
first_nameResulting output: "Hello, ".
Creating Additional Variables
Another example of variable creation:
food = <favorite_food>Example setup:
food = "pizza"
Printing this variable using an f-string:
Example output: "Hello, bro. You like pizza".
Email Address Creation
Users encouraged to create or input an email.
Example email:
my_email = "bro"
Numerical Data Types
Integers
Defined as whole numbers.
Example:
Age of a person:
age = 30Use case in code:
Print statement:
print(f"Your class has {num_of_students} students")Example output: "Your class has 30 students".
Characteristics of integers:
Should not be enclosed in quotes; otherwise treated as strings.
Can be used in arithmetic expressions.
Floats
Defined as floating-point numbers; contains a decimal portion.
Example:
Price of an item:
price = 10.99Print statement:
print(f"The price is ${price}")Example output: "The price is $10.99".
Currency may vary in user cases, but maintaining format is demonstrated:
Example: "The price is $10.99".
Example with grade point average:
gpa = 3.2Print statement:
print(f"Your GPA is {gpa}")Example output: "Your GPA is 3.2".
Distance example:
distance = 5.5Print statement:
print(f"You ran {distance} kilometers")Example output: "You ran 5.5 kilometers".
Booleans
Defined as logical values representing either true or false.
Example introduction:
is_student = True(if the scenario of being a student is true)Alternatively:
is_student = False(if graduated, for instance).Print statement to display Boolean:
print(f"Are you a student? {is_student}")Example output: "Are you a student? True".
Booleans are typically not output directly:
More often used in control flow with if statements.
If scenario:
Example condition:
if is_student:Action: “You are a student.”
Else clause for non-student scenario.
Further Examples of Booleans
For sale status example:
Variable:
is_for_sale = TrueConditional check:
if is_for_sale:Output: "That item is for sale."
Alternative:
If false: Output "That item is not available."
Conclusion
These programming concepts are foundational for variable management and data handling.
Emphasis on avoiding quotes around integers in order to maintain numerical data type operations.
Understanding and using f-strings for formatted output enhances readability and usability in outputs.