System Scripting Lab 06
Python Basics
Setup
mkdir lab06
cd lab06
idle3
Python Interactive Examples:
5 + 6 # 11
4 * 2 # 8
6 / 4 # 1.5
Variables
x = 10
print(x)
# Print with text
print("Value of x is:", x)
Exercise 1: Variables
1. Valid Variable Names
- ❌ 8number (cannot start with number)
- ✅ b
- ✅ counter
- ❌ #tango (invalid character)
- ❌ "bed" (string, not variable name)
- ✅ NUMBER
- ✅ Android_phone1
- ❌ this variable (spaces not allowed)
2. Data Types
- a = False → bool
- b = 3.7 → float
- c = 'Alex' → str
- d = 7 → int
- e = 'True' → str
- g = '17' → str
- i = '3.14159' → str
- j = "---------add---------" → str
Exercise 2: Boolean Operators
Given:
a = False
b = True
c = False
Results:
- b and c → False
- b or c → True
- not a and b → True
- (a and b) or not c or (not b and not (a or c)) → True
- not ((not b or not a) and c) or a → True
Comparisons (x = 3)
- 1 < x → True
- x >= 4 → False
- x == 3 → True
- x == 3.0 → True
- "Hello" == "Hello" → True
- 4.0/2.0 == 2 → True
- 3 < 7 and 4 > 5 → False
Exercise 3: User Input Script
# Request user name
name = input("Enter your name: ")
# Request year of birth
year = int(input("Enter your year of birth: "))
# Calculate age (assuming current year = 2026)
age = 2026 - year
# Output message
print("Welcome", name)
print("You are", age, "years old")
Exercise 4: Formatted Output
print("Twinkle, twinkle, little star,")
print("How I wonder what you are!")
print("Up above the world so high,")
print("Like a diamond in the sky.")
print("Twinkle, twinkle, little star,")
print("How I wonder what you are")
Note: Repeat the block again to match required output.
Exercise 5: Single Print Multi-line Output
# Get user input
name = input("Enter your name: ")
age = input("Enter your age: ")
address = input("Enter your address: ")
# Print in one statement
print("Name:", name, "\nAge:", age, "\nAddress:", address)
Explanation:
\n → New line character
- Single print used for multiple lines
Key Exam Tips
- Variable rules: No spaces, no starting with numbers
- Types: int, float, str, bool
- input() always returns string → use
int() if needed
- Boolean logic is commonly tested
- == vs = → comparison vs assignment
- \n is essential for formatting
- Indentation matters in Python