Day3-Facilitation Guide (Programming Statements)
Day3-Facilitation Guide (Programming Statements)
Programming Statements
Index
I. Recap
II. Programming Statements
III. User Input
IV. Comments in Program
I. Recap
In our last session we learned:
● Variables and Constants:Variables and constants are fundamental concepts in
programming and represent different types of data storage in a program.
● Operators and its Precedence:Operators in programming are symbols or
keywords that represent specific operations or actions to be performed on data.
Each operator has a precedence level, which determines the order in which
operators are evaluated in an expression. When an expression contains multiple
operators, the operator with higher precedence is evaluated first.
In this session we are going to learn about Programming Statements,User Input and
how to use Comments in Programming.
For example,
A series of English words, such as —
subject a need and does sentence a verb ?
The above has little meaning without syntax.
Now the above line is more understandable as we applied the sentence formation
rules of the English language.
If the syntax of a language is not followed, the code will not be understood by a
compiler or interpreter.
1.Assignment Statements:
Example : x = 10
2.Expression Statements:
Example: result = x + y
3.Print Statement:
Used to display output to the console.
4.Input Statement:
6.While Loop:
Syntax:
while condition:
# statements
# increment/decrement
count = 0
while count < 5:
print(count)
count += 1
7.For Loop:
Syntax:
for variable in sequence:
# statements
for i in range(5):
# Loop from 0 to 4
print(i)
Syntax:
Syntax:
try:
# statements that can throw error
except ExceptionType as object:
# statements for handling exception
try:
result = 10 / 0 # Attempt to divide by zero
except ZeroDivisionError as e:
print("Error:", e)
for i in range(10):
if i == 5:
break # Exit the loop when i equals 5
if i == 3:
continue # Skip the rest of the loop body for i equals 3
print(i)
13. Comments:
"""
This is a
multi-line comment
"""
These are essential Python programming statements that form the foundation of Python
programming. You can use these building blocks to create more complex programs and
applications by combining and modifying them as needed.
P.N - We will learn in detail about the programming statements such as
conditional statements, loops, functions, etc in the upcoming sessions
In this example:The input("Enter something: ") statement prompts the user to enter
something by displaying the text "Enter something: ".
The user enters text at the keyboard, followed by pressing the Enter key.
The input() function reads the entered text and assigns it to the user_input variable as a
string.
Finally, the program prints the user's input back to the console.
Note: that input() always returns a string, even if the user enters numerical values. If
you need the input as a different data type (e.g., integer or float), you should explicitly
convert it using functions like int() or float():
Another example of input() function which will accept your age and display the
age by adding 1
Output:
In this example, int(input()) converts the user's input to an integer, allowing you to
perform arithmetic operations with it.
Remember to handle user input with care, especially if the program expects specific
input formats or needs to handle potential errors (e.g., non-integer input when expecting
an integer). You may want to include error-checking and validation to ensure the input
meets your program's requirements.
Another example of input() function which will accept two number and display
their addition, subtraction and multiplication and division
We use the input() function to accept two numbers as input from the user. We convert
the input values to floating-point numbers using float() to handle decimal input.
We check if the second number is zero before performing division to avoid division by
zero errors. If the second number is zero, we display a message indicating that division
by zero is undefined.
Finally, we display the results of all four operations using formatted strings with print().
result = 42 # This comment explains the purpose of the following line of code
'''
This is a multi-line comment or docstring.
It can span multiple lines and is typically used for documentation.
'''
Args:
x (int): The first operand.
y (int): The second operand.
Returns:
int: The sum of x and y.
"""
return x + y
Comments are an important part of code readability and maintenance. They can help
others (and your future self) understand your code's logic, purpose, and usage. Good
commenting practices make it easier to collaborate on projects and debug code when
issues arise.
Exercise ChatGPT
1. Hi can you help me with a program in Python to accept two number from the
user and calculate their square.
2. I need to create a program to check if a number is even or odd. Please help
with a program in Python to accept a number from the user and using ternary
operator check whether the number is even or odd.