Python Tutorial : Comments and variables

Learn how comments clarify code and how variables store values with names and assignments. The tutorial covers naming rules, avoiding reserved words like type, and shows a practical example calculating a stock's price-to-earnings ratio by dividing price by earnings per share.
Chapters
In your last exercise, you may have noticed lines of code that started with a hashtag symbol. In Python, this symbol as the first character in a line indicates that the line is a comment. A comment is an annotation to the code that makes the code easier to understand and offers information to others. Reading the code in the last exercise, you also had a chance to see the code in the ipython shell and the code within scripts. The output displayed when the code is directly written in the ipython shell is slightly different when compared to the output displayed when the code is written in the script and then executed. As you can see here, in ipython shell, the output is displayed even without an explicit command to print it to screen. However, if you use the print command in the ipython shell, you can see that the output displayed is slightly different. In contrast, to generate an output of a script, you have to explicitly use the print command. If you do not specify the print command in this script, no output would be displayed. Variables are a very important part of programming. 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 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'll learn about in the next video, so should be avoided as a name for your own variable. 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 daytwo is associated with the number five. 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 price to earning ratio is defined as price divided by earnings. Since we've stored values in the variables price and earnings, we can calculate the price to earning ratio using these variables as shown here. Now let's try some examples.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







