First Week Note
First Week Note
Python
Python data types are classified into two main
categories.
1.1. Primitive Data Types
These are the basic, built-in types that cannot be
broken down further:
int: Integer numbers (e.g., 5, -3)
float: Floating-point numbers (e.g., 3.14, -2.5)
str: String of characters (e.g., "Hello", "Python")
bool: Boolean values representing True or False
1.2. Non-Primitive (or Composite) Data Types
List, tuple , dictionary and set.
2,Casting in Python
In Python, casting refers to converting one data
type to another. Here's a summary of allowed
conversions between common types:
2.1. int (Integer)
To float: Allowed.
To str: Allowed.
To bool: Allowed (0 is False, others are True).
2.2. float (Floating-Point)
To int: Allowed (truncates decimal part).
To str: Allowed.
To bool: Allowed (0.0 is False, others are True).
2.3. str (String)
To int: Allowed if the string represents a valid
integer.
To float: Allowed if the string represents a valid
float.
To bool: Allowed (empty string is False, non-empty
is True).
2.4. bool (Boolean)
To int: Allowed (True is 1, False is 0).
To float: Allowed (True is 1.0, False is 0.0).
To str: Allowed (True is 'True', False is 'False').
Note: Invalid conversions (e.g., non-numeric string
to int or float) will raise a ValueError.
3,Variables in Python
3.1. What is a Variable?
A variable is a container for storing data. It allows
you to store and manipulate values in a program.
Example: Assigning a Variable
name = "Alice" # Stores a string
age = 25 # Stores an integer
height = 5.7 # Stores a float
is_student = True # Stores a boolean
3.2. Variable Naming Rules
When naming variables in Python, follow these
rules:
✅ Allowed:
✔ Must start with a letter (a-z, A-Z) or underscore
(_) only
✔ Can contain letters, digits (0-9), and underscores
(_) only
✔ Case-sensitive (Age and age are different
variables)
✔ Use snake_case (recommended)
❌ Not Allowed:
✖ Cannot start with a number
2name = "Error" # ❌ Incorrect
✖ Cannot contain spaces
user name = "Alice" # ❌ Incorrect
✖ Cannot contain special characters (@, #, $, %,
etc.)
user@name = "Alice" # ❌ Incorrect
✖ Cannot use Python keywords (if, while, class,
etc.)
class = "Python" # ❌ Incorrect
✖ Cannot use hyphens (-) (Python treats it as
subtraction)
user-name = "Alice" # ❌ Incorrect
Examples of Correct Naming:
✅ Snake Case (Recommended)
first_name = "John" total_price = 100
3.3. Assigning and Reassigning Variables
Python allows dynamic typing, meaning a variable
can hold different types at different times.
x = 10 # x is an integer
print(x) # Output: 10
x = "Hello" # Now x holds a string print(x) #
Output: Hello
3.4. Using variables for user input
Python allows storing user input in variables.
name = input("Enter your name: ")
print( name )
If taking numerical input, convert it to int or float:
age = int(input("Enter your age: ")) # Converts
input to integer print("Next year, you will be", age
+ 1)