Comments #
- In Python, a line that starts with a hashtag symbol (#) is considered a comment.
- A comment is a note within the code that explains what the code does.
- Comments make the code easier to understand and provide information to others who read the code.
- Enhancing Readability: Comments make code easier to read and understand. They explain what specific sections of code do, which is particularly helpful when you return to your code after some time or when others read your code.
- Debugging Assistance: Comments can be used to temporarily disable code during debugging without deleting it.
- Documentation: They serve as internal documentation for your code, explaining logic, methodology, and decisions made during development.
Comments in Finance: #
- Clarifying Financial Logic: Comments can explain complex financial formulas or calculations, making it easier for others (or yourself at a later date) to understand the logic behind the code.
- Recording Assumptions: When building financial models, assumptions are often made. Comments can document these assumptions directly in the code.
- Tracking Modifications: In financial models, tracking changes is crucial. Comments can indicate why and when changes were made, providing a history of modifications.
A line that starts with a hashtag symbol (#) is considered a comment. The following is an example of a comment:
# Calculate the Price-to-Earnings (P/E) ratio
# P/E ratio = Market Price per Share / Earnings per Share
Variables #
- Variables are fundamental in programming.
- They store information that can be referenced and used throughout your code.
- Descriptive variable names help make programs easier to read and understand.
- Variables consist of two parts: the name and the value.
- The name can include letters (both uppercase and lowercase), digits, and underscores, but it cannot start with a number.
- Some names are reserved in Python, such as \’type\’, which has a specific meaning in the language and should be avoided for your own variables.
- Data Storage: Variables store data values that can be used and manipulated throughout your code.
- Flexibility: By using variables, you can write more flexible and reusable code. For example, changing the value of a variable in one place updates it throughout the code.
- Descriptive Coding: Well-named variables make your code self-explanatory, which enhances readability and maintainability.
Variables in Finance:
- Storing Financial Data: Variables can store financial data, such as stock prices, interest rates, and trading volumes, allowing for easy manipulation and analysis.
- Building Financial Models: Variables are essential in creating financial models, where they can represent inputs (like initial investments, growth rates) and outputs (like future value, returns).
- Performing Calculations: Variables allow you to perform and store the results of calculations, such as calculating net present value (NPV), internal rate of return (IRR), or price-to-earnings (P/E) ratios.
Variable Example #
- To assign a value to a variable, you first name the variable and then use the equal sign (=) to assign a value.
- For instance, the variable
week_3
can be assigned the value 10 by writingweek_3 = 10
.
Using Variables to Evaluate Stock Trends #
- Price-to-earnings (P/E) ratios are a common metric for evaluating stocks, indicating how much investors are willing to pay per dollar of earnings.
- Suppose we set the variable
stock_price
to 150, representing a market price of $150 per share, and the variableearnings_per_share
to 8, representing stock earnings of $8 per share. - The P/E ratio is calculated as price divided by earnings.
- Using the variables
stock_price
andearnings_per_share
, we can compute thepe_ratio
as shown here:pe_ratio = stock_price / earnings_per_share
.
The following is an example of the code:
# Assign values to variables
stock_price = 150 # Market price of the stock in dollars
earnings_per_share = 8 # Earnings per share in dollars
# Calculate the P/E ratio
pe_ratio = stock_price / earnings_per_share
# Output the P/E ratio
print(pe_ratio) # This should print 18.75