Python Basics Summary
-
Hello World Program
- Use
print("Hello, World!")to display text.
- Use
-
Syntax Errors
- Occur when Python can't understand your code due to incorrect structure.
- Example: missing brackets, colons, or using wrong keywords.
- print("Hello" # ❌ SyntaxError: missing closing parenthesis
- Variable Naming Rules
- Must start with a letter or underscore (
_) - Can include letters, numbers, and underscores
- Cannot use Python keywords (like
class,if,def) - Case-sensitive (
Name≠name) student_name = "Aisha" # ✅ Valid 1name = "Ben" # ❌ Invalid-
Using Variables
- Variables store data like text, numbers, or results of calculations.
name = "Aisha" score = 85 total = score + 10
- Input from Command Prompt
- Use
input()to get user input. - Always returns a string, so convert to
intorfloatif needed. name = input("Enter your name: ") age = int(input("Enter your age: "))-
Comments in Python
- Used to explain code or leave notes.
- Single-line:
# This is a comment - Multi-line:
''' This is a multi-line comment ''' - Data Types
str: text (e.g.,"Hello")int: whole numbers (e.g.,10)float: decimal numbers (e.g.,3.14)-
Assignment Statements
- Use
=to assign values to variables.
- Use
-
If-Else Statements
- Make decisions using
if,elif, andelse. score = 75 if score >= 50: print("Pass") else: print("Fail")
- Make decisions using
-
BIDMAS Rule in Arithmetic
- Python follows Brackets, Indices, Division, Multiplication, Addition, Subtraction.
result = (3 + 2) * 4 ** 2 / 2 - 5
-
Comparison Operators
==,!=,>,<,>=,<=for comparing values.
-
Indentation
- Python uses indentation to define blocks of code (e.g., inside
if,for,def).
- Python uses indentation to define blocks of code (e.g., inside
-
Types of Errors
- Syntax Error: Code structure mistake
- Runtime Error: Error during execution (e.g., divide by zero)
- Logic Error: Code runs but gives wrong result
-
Loops
forloop: repeat a known number of timesfor i in range(5): print(i)whileloop: repeat while a condition is truecount = 1 while count <= 5: print(count) count += 1
No comments:
Post a Comment