Programming Basics: Variables, Input, Conditions, While Loop
1. Variables
Variables are containers for storing data values.
In Python, you can create a variable just by assigning a value to it.
Example:
x=5
name = "Alice"
pi = 3.14
2. Input
To get input from the user, use the input() function.
It always returns a string, so you may need to convert it.
Example:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name, "! You are", age, "years old.")
3. Conditions (if/else)
Conditions allow you to run different code depending on whether something is True or False.
Example:
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
4. While Loop
A while loop repeats a block of code as long as a condition is True.
Programming Basics: Variables, Input, Conditions, While Loop
Example:
count = 1
while count <= 5:
print("Count is", count)
count += 1
This loop will print numbers from 1 to 5.