0% found this document useful (0 votes)
3 views3 pages

2 Code Week 1 Practical Session Python Basics

This document outlines the second practical session of a course on Basic Python Programming and its Application in Finance, focusing on Jupyter Notebook basics. It covers essential shortcuts for coding and navigating cells, as well as the use of comments in Python. Additionally, it emphasizes best practices for writing comments to enhance code clarity and maintainability.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

2 Code Week 1 Practical Session Python Basics

This document outlines the second practical session of a course on Basic Python Programming and its Application in Finance, focusing on Jupyter Notebook basics. It covers essential shortcuts for coding and navigating cells, as well as the use of comments in Python. Additionally, it emphasizes best practices for writing comments to enhance code clarity and maintainability.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

5/5/25, 3:03 PM SWAYAM-Week 1-Session 2

Basic Python Programming and its Application in


Finance
Week 1-Practical Session 2

Hi everyone
Welcome to Basic Python Programming and its
Application in Finance
This is our second session on Jupyter basics
It covers Shortcuts, and comments discussed in Unit 2
Let us get started. I am sure you will enjoy the learning process

Two functions of Cells-Coding and writing the text for


explanation
In [1]: # let us start with the first line of code

In [2]: print("Hello world")

Hello world

In [3]: print("Hello world")


print("Hello Learners")

Hello world
Hello Learners

In [7]: # Python is Case Sensitive

In [4]: print("Hello world")

Hello world

These things will help you in the process of coding-


1. You can switch between edit mode and command mode using "enter"
and "esc" buttons

2. Use the Up and Down arrow keys to navigate between cells.

3. Press A to insert a new cell above, or B to insert one below.

4. Press M to change a cell to Markdown.

5. Press Y to change it back to a code cell.

6. Press D twice (that is, D + D) to delete a cell.


[Link] 1-Session [Link] 1/3
5/5/25, 3:03 PM SWAYAM-Week 1-Session 2

7. Press Z to undo a deletion.

8. You can also select multiple cells by holding Shift and pressing the Up or
Down keys.

9. Once multiple cells are selected, you can press Shift + M to merge them
into one.

10. Shift+L toggles the line numbers

11. Get all the shortcuts in one place

In [8]: # Just type h when the cell is in the command mode

12. Use the help key frequently

In [5]: help("keywords")

Here is a list of the Python keywords. Enter any keyword to get more help.

False class from or


None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not

In [6]: help("print")

Help on built-in function print in module builtins:

print(*args, sep=' ', end='\n', file=None, flush=False)


Prints the values to a stream, or to [Link] by default.

sep
string inserted between values, default a space.
end
string appended after the last value, default a newline.
file
a file-like object (stream); defaults to the current [Link].
flush
whether to forcibly flush the stream.

In [7]: help("sum")

Help on built-in function sum in module builtins:

sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers

When the iterable is empty, return the start value.


This function is intended specifically for use with numeric values and may
reject non-numeric types.

[Link] 1-Session [Link] 2/3


5/5/25, 3:03 PM SWAYAM-Week 1-Session 2

Comments in Python

In [13]: # Single line comment

In [38]: # print("This line will not print")

In [15]: print("This line will print")

This line will print

In [16]: print("This line will print") # This is an in-line comment to explain

This line will print

In [20]: #Multiline comment-Use triple inverted commas instead of hash in every line

In [41]: """
This is a multi-line comment
It can span multiple lines
Python ignores it at runtime
"""

' \nThis is a multi-line comment\nIt can span multiple lines\nPython ignores it at


Out[41]:
runtime\n'

In [42]: """
asdsds
sadfdsf
sdfdsfdsf

"""

'\nasdsds\nsadfdsf\nsdfdsfdsf\n\n\n'
Out[42]:

Why Use Comments?


• To explain what and why something is done • To make code easier to maintain • To
temporarily disable parts of code

Best Practices
• Keep comments clear and concise • Don’t comment obvious code • Use them to explain
logic, not syntax

End of session 2

[Link] 1-Session [Link] 3/3

Common questions

Powered by AI

Comments should be avoided when the code is self-explanatory or when they describe what the code is doing in terms of syntax, since this can clutter the code and reduce readability. They should also not be used for obvious code that can be easily understood without additional explanation. Instead, focus comments on explaining the logic behind the code, which enhances understanding and maintainability .

Multiline comments in Python are useful for providing explanations or disabling blocks of code that extend over multiple lines. They are enclosed within triple quotes (""") and are ignored by Python during runtime. Unlike single-line comments, which begin with a hash (#) and are used for brief explanations or notes, multiline comments offer a way to document more extensive information without repeated use of hashes .

Jupyter Notebook shortcuts enhance coding efficiency by allowing quick navigation and editing within the notebook without the use of a mouse. For example, pressing 'A' inserts a new cell above the current one, while 'B' inserts one below. 'Enter' and 'Esc' are used to switch between edit and command modes. You can delete a cell with 'D + D', and 'Shift + M' merges selected cells. These shortcuts streamline the workflow, allowing for faster and more intuitive development .

Switching between edit mode and command mode in Jupyter Notebook aids workflow by allowing different sets of actions: edit mode enables typing code or text within a cell, while command mode allows for cell-level actions such as adding, deleting, or moving cells. Enter switches to edit mode, while Esc returns to command mode. These modes allow for efficient code organization and testing .

Comments in Python are significant because they help explain what and why code is written, making the code easier to maintain and understand. Best practices include: keeping comments clear and concise, avoiding comments for obvious code, and using comments to explain the logic rather than syntax. Additionally, comments can be used to temporarily disable parts of the code .

The 'print' function in Python outputs values to a stream or sys.stdout by default, which can include arguments separated by a space, and it ends with a newline character. The 'sum' function, on the other hand, returns the sum of an iterable of numbers starting from the defined start value (default is 0). The primary difference lies in their purpose: 'print' is used for outputting data, while 'sum' is used for computation .

You might also like