Python Tutorial : Comments and variables
Key Takeaways
This video tutorial covers the basics of comments and variables in Python, including how to use the hashtag symbol for comments, assign values to variables, and use variables in finance examples.
Full Transcript
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 a Python 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 parts the name and the value the name of a variable can include letters upper and lowercase 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 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 day underscore two is associated with an number five let's look at a finance example where variables are useful priced are earning ratios are a common way to evaluate stocks it shows how much investors are willing to pay per dollar of earnings first stock will set the variable price equal to 200 representing the market price of the stock at $200 per share and the variable earnings to five representing stock earnings of $5 per share the price to earnings 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 earnings ratio using these variables as shown here now let's try some examples
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/intro-to-python-for-finance at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
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 the 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 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 it should be avoided as a name for your own variable.
An example of a variable in Py
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI