At the core of any Object-Oriented Programming (OOP) language are variables and data types. These are the foundation that everything else is built upon. This tutorial will cover the following learning objectives:
What Variables Represent and How They Work
What Data Types are Available in Python
What Variables Represent and How They Work
Summary
A variable is a type of storage unit that has an assigned name and holds data that you can change or use later. The list below are all examples of variables: age = 21 # integer data type name = "Joshua" # string data type percentage = 75.66 # float data type
In Python, data types are inferred, thus making it unnecessary to explicitly define a variable's data type when declaring it.
What Data Types are Available in Python
Summary:
The type() function is used to output the data type of a specific variable. This function has the following syntax: type([variable]) OR type([value])
An Integer is a positive or negative whole number; without a decimal point. The variables below all hold integers: age = 21 quantity = 10 attendance_count = 500
The int() function is used to convert a float into an integer. It does this by truncating all decimal places and keeps only the whole number on the left side of the decimal. If the number is less than 0, the new value will be 0. This function has the following syntax: int([variable]) OR int([value])
A Floating Point Number (float) is a real number; a number with finite number of decimal points. The variables below all hold floats: percentage = 98.07 final_grade = 97.77786 accidents_per_million = 107.1213
The float() function is used to convert an integer into a float. This function has the following syntax: float([variable]) OR float([value])
A Boolean value is used to represent a binary condition (e.g., True/False, Yes/No, Correct/Incorrect). The variables below all hold Boolean values: has_cancer = False is_awesome = True
When assigning a variable a Boolean value, you MUST keep the Boolean in sentence case (capitalize the first letter). If you don't, a NameError will be returned.
A String is a text value compsoed of a sequence of characters. The variables below all hold strings: name = "Thomas" major = 'Computer Science' description = 'I am awesome, good looking, and tall'
The str() function is used to convert any type of variable or raw value into a string. This function has the following syntax: str([variable]) OR str([value])
The print() function, as shown in the video, is used to output a message to the console. This can be either a variable, a raw value, or a combination of both. When combining non-string variables with strings, you'll need to convert the variable to the string data type using the str() function. Example: print(str(age) + "years old")
NOTE: When defining a variable with an apostrophe or a single quote, you'll need to either place the string between double quotes OR escape the character in question. Example: sentence = "I'm writing Python code." OR sentence = 'I\'m writing Python code.'
Exercise
Congratulations! You just completed the Variables and Data Types Tutorial! To help test your knowledge, let's
practice creating some variables with various data types and conduct some type casting.
Instructions:
Open your IDE (e.g., VS Code, PyCharm, Spyder).
Create a New Python File. Name it "variables.py"
In the Newly Created Python File, complete the following tasks:
Declare a variable named "price" and give it a value of 10.50.
Declare a variabled named "item_name" and give it a value of "Sandwich".
Declare a variable named "discounted" and give it a value of True.
declare a variable named "discount_rate" and give it a value of 25.
Change the "discount_rate" variable to be a float.
Print out the following message: "Our [item_name] costs $[price]. Discount? [discounted]. The Discount is currently [discount_rate]%"
Exercise Completed! Click here to view the answers.
Have any issues with the above exercise? Post your question on Discord!