Variables and Data Types Study Guide
UNIT 2: VARIABLES AND DATA TYPES
STUDY GOALS
- Demonstrate how to use variables in Python and how to assign them different values.
- Use various numerical data types, string, and character data types.
- Organize and manipulate collections of data.
- Implement basic file input/output operations.
HARD-CODING
- Definition: A value is hard-coded if data are fixed and cannot be changed without editing the program.
CASE STUDY: KYLE AND MORGAN
- Kyle and Morgan chose Python for their soccer analytics and player improvement application due to several factors:
- Ease of use and rapid prototyping capabilities.
- The powerful features of Python that allow completion of the project without needing to transition to another language.
- Extensive resources available in the Python community.
- Libraries for data manipulation, mathematical operations, data visualization, AI, and machine learning will be invaluable for handling massive amounts of soccer player data.
- Key goals for the application:
- Collect data on players' game conditions, running speed, and measurable characteristics.
- Analyze data to aid in identifying areas for improvement.
VARIABLES AND VALUE ASSIGNMENT
Introduction to Variables
- Variables are containers that hold information.
- Example: To store player weight of 73 kg, a variable like
weight can be created.
- Importance of variables:
- Avoids hard-coding values; allows for updates without code edits.
- Example: Instead of hard coding a player's weight, one can use a variable that can be updated if the player's weight changes.
RESERVED WORDS
- Definition: Some words have specific meanings in programming languages and cannot be reused by programmers. These are known as reserved words.
- Example: In Python, the word
print is a reserved word.
VARIABLE NAMING RULES IN PYTHON
- A variable name must:
- Start with a letter or an underscore (_).
- After the first character, can contain letters, numbers, and underscores.
- Valid examples:
weight, _weight_ - Invalid examples:
5weight, $weight (contains a misplaced character).
CREATING VARIABLES IN PYTHON
- Open JupyterLab by typing
jupyter lab in Anaconda Prompt. - Open a Python3 Console.
- To create a variable: Type
weight = 73 and hit Enter.
WORKING WITH VARIABLES
- To check a variable’s value, type
print(weight); it should output 73. - The assignment operator
= assigns values from right to left. - Example:
player_weight = 73 results in a variable player_weight being created and assigned the value of 73. - Invalid syntax error is returned if naming rules are violated (e.g., typing
$weight).
CATEGORIES OF VARIABLE NAMES
- Variables can be categorized as:
- Valid/Conventional: Good naming practices followed (e.g.,
player_weight). - Valid/Unconventional: Rules followed but names not descriptive enough (e.g.,
weight). - Invalid: Breaks naming rules (e.g.,
$weight).
ASSIGNMENT OPERATOR
- Processes from right to left:
- Example:
player_weight = 70 + 3 assigns 73 to player_weight.
- Code reflecting modified variables:
player_weight = player_weight + 5 updates player_weight from 73 to 78.
DATA TYPES
Numerical Types
- Integers (int): Represents whole numbers (positive, negative, or zero).
- In Python 3, the int type has no size limit.
- Floating Point Numbers (float): Represents decimal values.
- Example:
player_weight = 73.6, another_variable = -15.82715, etc.
- Scientific Notation: Depicted as
a * 10^b where a is a number between 1 and 10.- Example: $4.5 imes 10^7$ can be expressed as
4.5e7.
SPECIAL NUMBERS
- Imaginary Numbers: Represented as a + bj, where j is the square root of -1.
- Example:
complex_number = 1 + 6j.
- Hexadecimal and Octal: Represented with prefixes
0x (for hex) and 0o (for octal).
STRING DATA TYPE
- Strings (str): Represent a series of characters.
- E.g.,
player_name = "Franz Beckenbauer".
- Strings in Python are immutable (cannot be changed once assigned).
- Escape sequences allow for inclusion of special characters in strings:
- E.g., to include quotation marks, use
".
STRING OPERATIONS
- Common Functions
len(): Returns length of string.my_string.count('o'): Counts occurrences of 'o'.- Convert to lowercase:
my_string.lower().
- Concatenation: Strings can be combined using
+. - Formatting: Use curly braces for placeholders:
- E.g.,
my_str = "My name is {name} and I'm {age}".format(name="Joe", age=28).
- Extracting Substrings: Can be achieved through indexing and slicing.
COLLECTIONS IN PYTHON
- Python supports various data collection types that allow storage of multiple items:
- Lists: Ordered and mutable.
- Sets: Unordered, unindexed, and do not allow duplicates.
- Dictionaries: Unordered and mutable collection of key-value pairs.
- Tuples: Ordered but immutable.
WORKING WITH SETS
- Creating a set:
my_set = {“Red”, “Green”, “Blue”}. - Adding and removing elements: Use
add() and remove() methods. - Example: