# This is a comment in Python. Comments are not executed and are used for documentation.
# Assign values to variables 'a' and 'b'
a = 10
b = 20
# Perform addition and store the result in variable 'c'
c = a + b
# Print the value of 'c' to the console
print("The sum of a and b is:", c)
# Assign a string to variable 'x'
x = "Hello, World!"
# Print the value of 'x' to the console
print(x)
Explanation:
-
Comments: Lines starting with the
#
symbol are comments. Comments are not executed and are used for adding explanations and documentation to the code. -
Variable Assignment: In Python, variables are created by assigning values to them. Here, we assign the integer value
10
to the variablea
and20
to the variableb
. -
Mathematical Operation: We add the values of variables
a
andb
together and store the result in the variablec
. -
Printing: The
print()
function is used to display output on the console. In the firstprint
statement, we display a message along with the value of variablec
. The+
operator is used to concatenate the message and the value. In the secondprint
statement, we simply print the content of variablex
.
When you run this code, you'll see the following output in the console:
The sum of a and b is: 30
Hello, World!
Code Block 1 - Understanding Variables in Python:
# Variable Assignment
t1 = "amit"
t2 = "simran"
t3 = "vidya"
# Printing Variables
print("Hello", t1)
print("Hello", t2)
print("Hello", t3)
# Variable Overwriting
t1 = "amit verma"
print(t1)
t1 = "India"
print(t1)
Explanation:
- Variables
t1
,t2
, andt3
are assigned names: "amit," "simran," and "vidya." - The
print
command is used to display a greeting followed by the name assigned to each variable. - The variable
t1
is overwritten with "_amit verma" and later with "India." - The
print
command is used to display the current value oft1
.
# Variable Initialization
a = 1
# Executing a Sequence
a = a + 1
print(a)
a = a + 1
print(a)
a = a + 1
print(a)
Explanation:
- Variable
a
is initialized with the value 1. - A sequence of instructions is executed to increment
a
and display its value. - The code block demonstrates how instructions are executed sequentially.
Code Block 3 - Taking Inputs from the User:
# Input from User
n = input("What is your name?")
# Greeting
print("Hello", n, "how are you", n)
print("I hope life is treating", n, "well")
Explanation:
- The
input
function is used to take a user's input (their name) and assign it to the variablen
. - A greeting message is then printed with the user's name included, demonstrating input and variable usage.
Code Block 4 - Taking User Input with Customized Messages:
# Input from User
name = input("What is your name?")
# Greeting with Customized Message
print("Hello", name, "how are you", name, "? I hope life is treating", name, "well.")
Explanation:
- The code is similar to the previous one but includes customized messages to the user, using their name.
- This demonstrates how to take user input and display personalized messages.
- Discount Calculation:
# Input the cost of the item
cost = input("What is the cost? ")
# Convert the input (which is a string) to an integer
d = int(cost)
# Calculate the discounted price (10% discount)
discounted_price = 0.9 * d
# Print the discounted price
print("The cost after a 10% discount is:", discounted_price)
- Input the cost of the item.
- Convert the input (which is a string) to an integer using
int()
. - Calculate the discounted price with a 10% discount (multiply by 0.9).
- Print the discounted price.
- If Conditions:
# Input the user's choice
choice = input("Please enter your choice: ")
# Convert the input (which is a string) to an integer
d = int(choice)
# Use an if statement to check the value of the choice
if d == 1:
print("You entered the number one.")
elif d == 2:
print("You entered the number two.")
elif d == 3:
print("You entered the number three.")
else:
print("You entered an unrecognized number.")
- Input the user's choice.
- Convert the input (which is a string) to an integer using
int()
. - Use an if statement to check the value of the choice.
- Print a corresponding message based on the choice entered. If the choice is not 1, 2, or 3, it prints an "unrecognized number" message.
- Basic Loop Using "for" in Python:
# A basic "for" loop to print a statement multiple times
for i in range(10):
print("Hi, how are you?")
# The "range" function generates numbers from 0 to 9 (10 times).
- This code demonstrates a simple "for" loop that repeats the print statement 10 times.
range(10)
generates numbers from 0 to 9 (10 times), and the loop iterates through each of these numbers.- In each iteration, it prints the statement "Hi, how are you?"
- "for" Loop with Variable and Arithmetic:
# Using a "for" loop with a variable to print numbers and their multiples
answer = 0
for i in range(5):
answer += i
print("For I in range 5: answer =", answer)
# The loop adds numbers from 0 to 4 to the "answer" variable.
- This code illustrates a "for" loop with a variable (
answer
) to calculate the sum of numbers from 0 to 4. - The variable
i
takes on the values generated byrange(5)
, which includes 0, 1, 2, 3, and 4. - In each iteration, it adds
i
to theanswer
variable and prints the updated value ofanswer
.
- Sum of Numbers Using "for" Loop:
# Using a "for" loop to calculate the sum of numbers from 1 to 10
answer = 0
for i in range(11):
answer += i
print("The sum of numbers from 1 to 10 is:", answer)
- This code demonstrates a "for" loop to calculate the sum of numbers from 1 to 10.
- The loop iterates through the numbers generated by
range(11)
, which includes 0, 1, 2, 3, and so on, up to 10. - In each iteration, it adds the value of
i
to theanswer
variable to compute the total sum. - The final result is displayed as "The sum of numbers from 1 to 10 is:" followed by the sum (55).
# Displaying multiplication tables using "for" loops
t = int(input("Enter the table number: "))
for i in range(1, 11): # Loop through numbers from 1 to 10
result = t * i
print(f"{t} x {i} = {result}")
- In this code, you provide a number (e.g., 7) as input to
t
, which will represent the table you want to display. - The "for" loop iterates through numbers from 1 to 10, and for each number (i), it calculates the product (result) of
t
andi
. - It prints the multiplication statement showing the table, the multiplier, and the result.
# Simulating a doctor's clinic using a while loop
n = 1 # Initialize token number
c = 1 # Initialize a control variable
print("Hello everyone, we are starting.")
while c == 1:
print(f"Token number {n} may please come in.")
c = int(input("Continue? (0 to stop, 1 to continue): ")) # Convert to integer
if c == 1:
n += 1
print("Thank you, this is the end of our day.")
- In this code, it simulates a doctor's clinic scenario using a while loop.
- It initializes
n
as the token number andc
as a control variable, both starting at 1. - It enters a loop with the condition
c == 1
, which means it will continue as long asc
is equal to 1. - Inside the loop, it prints the token number and asks if the clinic should continue (0 to stop, 1 to continue).
- The user's input for
c
is converted to an integer to ensure it's treated as a number. - If the user enters 1 to continue,
n
is incremented, and it goes to the next patient. - When the user enters 0, the loop exits, and the program thanks everyone for the day.
This code shows how a while loop can be used in a practical scenario, and it also demonstrates the use of user input within the loop. Practice these examples to solidify your understanding of loops and conditions.