Search . . .
MODULE 2 - LECTURE 6
COMMENTS AND USER
INTERACTION
AETN 2302:APPLIED PROGRAMMING - I
Noora Shifa
College of Engineering and Technology
Winter 2024
Noora Shifa AETN2302: Lecture 6 Jan 2025
COMMENTS
In Python, a comment is a piece of text that
begins with # and are used to leave additional
information in the source code.
Comments can be important when you are
reading your own code after some time, and
when others are reading your code to help them
understand what your programs does
Noora Shifa AETN2302: Lecture 6 Jan 2025
COMMENTS IN PYTHON
• What are Comments?
⚬ Comments are notes added to code, meant for humans rather than Python.
⚬ They are useful for explaining how code works, describing variable
meanings, and keeping information like the author or date.
⚬ Python ignores comments completely when running the code.
• Why Use Comments?
⚬ To explain code to others (or yourself).
⚬ To mark pieces of code that you might want to modify later.
⚬ To make code more readable and understandable.
Noora Shifa AETN2302: Lecture 6 Jan 2025
HOW TO WRITE COMMENTS IN PYTHON
Single-line Comment: Start with a # sign.
Multi-line Comments: Each line must start with a #.
Noora Shifa AETN2302: Lecture 6 Jan 2025
BEST PRACTICES FOR COMMENTING:
• Use clear, concise comments to explain tricky parts of the code.
• Avoid over-commenting – too many comments can make code
harder to read.
• Use self-commenting variables. For example: Use square_area
instead of aunt_jane. Well-named variables reduce the need for
comments.
• Commenting Out Code: Use comments to temporarily disable parts
of the code during testing:
Tip: Use the keyboard shortcut: CTRL + / (Windows) or CMD + / (Mac OS) to
comment or uncomment multiple lines at once
Noora Shifa AETN2302: Lecture 6 Jan 2025
INTERACTION WITH THE USER
The input() function allows users to enter
data through the console and returns the
input to the program, enabling the program
to use the provided data.
Noora Shifa AETN2302: Lecture 6 Jan 2025
INTRO TO THE INPUT() FUNCTION
• What is input()?
⚬ The input() function is used to get user
input from the console.
⚬ It is the opposite of the print() function,
which displays data.
• Why Use input()?
⚬ It makes the program interactive by
allowing the user to provide data.
⚬ The data entered is then returned to the
program for further use.
Noora Shifa AETN2302: Lecture 6 Jan 2025
HOW INPUT() WORKS
• How does input() function?
⚬ The program prompts the user to input data.
⚬ The user types input and presses Enter.
⚬ The inputted data is captured and returned as a
string.
• Example:
⚬ The program waits for the user's input and stores
it in name.
⚬ The print() function then uses this input to greet
the user.
You must assign the result to a variable, or the input will be lost.
Noora Shifa AETN2302: Lecture 6 Jan 2025
THE INPUT() FUNCTION WITH AN ARGUMENT
• How to Prompt the User:
⚬ You can pass a message directly to the
input() function as an argument.
⚬ This message will appear on the console
before the user is prompted to enter input.
• The prompt message is passed as a string
argument to input().
Important Note: You cannot perform
• Result Type: The result of the input() function is
arithmetic operations directly on the
always a string. Even if the user enters a input result
number, the value is captured as a string.
Noora Shifa AETN2302: Lecture 6 Jan 2025
PROHIBITED OPERATIONS
ON INPUT()
If you attempt to do arithmetic operations on a string, Python will raise a TypeError.
Noora Shifa AETN2302: Lecture 6 Jan 2025
TYPE CASTING (TYPE
CONVERSIONS)
Type casting is the process of converting data from
one type to another
Even though by default, input() returns a string, you
can cast the input to int or float to allow
mathematical calculations.
Noora Shifa AETN2302: Lecture 6 Jan 2025
TYPE CASTING IN PYTHON
• Type Conversions:
⚬ int(): Converts a string into an integer.
■ Example: int("10") → 10
⚬ float(): Converts a string into a floating-
point number, which allows for decimal
values.
■ Example: float("3.14") → 3.14
• If the string contains characters that cannot be
Direct Use:
converted (like letters), Python will raise a You can pass the input() result directly into int()
or float(), eliminating the need for an
ValueError. intermediate step. This saves time and keeps your
code clean.
Noora Shifa AETN2302: Lecture 6 Jan 2025
STRING OPERATORS IN PYTHON
• Concatenation (+): Combines two strings into
one.
⚬ string + string
• Replication (*): Repeats a string a specified
number of times.
⚬ string * number
• Converting Numbers to Strings (str()):
Converts numbers to strings for easier output
formatting.
Noora Shifa AETN2302: Lecture 6 Jan 2025
END OF
MODULE 2!