You can type the parts on different lines or use \n
Define a variable
Python variables are containers for storing data values.
What is a Value?
A value is one of the most basic things in any program works with. A value may be characters i.e. 'Hello, World! ' or a number like 1,2.2 ,3.5 etc. Values belong to different types: 1 is an integer, 2 is a float and 'Hello, World!
IF statements
A Python IF statement evaluates a condition and executes the indented block of code underneath it if the condition is true.
Here’s an example:
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are not an adult.")
This code checks if the user’s age is 18 or older and prints a message accordingly.
How to accept an answer in a python program
To accept an answer in a Python program, you can use theinput()function to receive user input as a string, which you can then process or evaluate as needed.
user_input = input("Please enter your name: ")
print("Hello, " + user_input)
If-Else Statement
A Python IF-ELSE statement allows for two possible paths of execution: the code under IF runs if the condition is true, and the code under ELSE runs if it’s false.
Here’s an example:
number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
This code determines whether a user-entered number is even or odd.
elif statement?
In Python, the ELIF statement, short for ‘else if’, allows for multiple conditions to be checked in sequence after an IF statement; if a condition is true, the corresponding code block executes. Here’s an example:
score = int(input("Enter your score: "))
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
This code assigns a grade based on the score input by the user.
time.sleep() statement
The time.sleep() statement in Python pauses the execution of the current thread for a specified number of seconds, allowing for timed delays in a program’s flow.
import time
print("Hello")
time.sleep(2) # Pauses for 2 seconds
print("World")
This code will print “Hello”, wait for 2 seconds, and then print “World”.
random module?
In Python, the random module provides functions that generate pseudo-random numbers, such as random.randint() to get a random integer within a range.
import random
print(random.randint(1, 10)) # Prints a random integer between 1 and 10
This code will output a random number between 1 and 10 each time it’s run.
print a variable within a string
In Python, you can print a variable within a string by using string formatting methods like f-strings, str.format(), or concatenation.
Here’s an example using f-strings:
name = "Alice"print(f"Hello, {name}!") # Output: Hello, Alice!
python concatenation
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name) # Output: John Doe
This code combines first_name and last_name into full_name and prints it.