Variables
1. Variables
Variables are a very important part of programming.2. Define variables
They can be used to store information that can be referenced and used in your code. This allows you more flexibility in programming. The best variables are named descriptively so programs can be more easily read and understood. Variables consist of two parts: the name and the value. The name of a variable can include letters (upper and lower case), digits, and underscores. Variables that start with numbers are not allowed in Python. Some names are reserved in Python, for example, 'type' already means something specific in Python (which you will learn in the next video), so should be avoided as a name for your own variable.3. Defining variables in Python
An example of a variable in Python is shown here. To assign a value to a variable, we name the variable first and use the equal sign to assign it a value, in this case, the variable day_2 is associated with the number 5.4. Using variables to evaluate stock trends
Let's look at a finance example where variables are useful. Price to earning ratios are a common way to evaluate stocks. It shows how much investors are willing to pay per dollar of earnings for a stock. We'll set the variable price equal to 200 representing the market price of the stock at $200 per share and the variable earnings to 5 representing stock earnings of $5 per share. The P/E ratio is defined as price divided by earnings. Since we've stored values in the variables price and earnings, we can calculate the pe_ratio using these variables, as shown here.5. Let's practice!
Now, let's try some examples.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.