Docsity

Python Variables & Data Types: Ultimate Student Revision Guide, Lecture notes of Computer Science

Toggle sidebar
bg1
CS101: PYTHON VARIABLES & DATA TYPES
Ultimate Student Revision & Quick Exam Prep Guide
1. Python Variables
In Python, a variable is basically like a container or a storage box where you can keep any data or
value you want.
📦 Real-World Example (Kitchen Logic):
Think of your kitchen at home. You probably have a jar labeled for "Sugar", and inside that jar,
you keep sugar. Variables work exactly the same way in coding! We give a clear name to a box
(the variable), and then we put a value inside it.
The Best Part About Python: Unlike other languages like C, C++, or Java, you don't need to
manually declare or tell Python what kind of data you are going to store in the box ahead of time.
Python is super smart—whatever value you put inside the variable, it automatically figures out the
type and sets the box up perfectly.
x = 5 # 'x' is a box storing the number 5
name = "Ali" # 'name' is a box storing the text "Ali"
2. Data Types in Python
To store different kinds of information, Python majorly uses 4 basic built-in data types:
int (Integer): This stores pure whole numbers without any decimal points. It can be positive or
negative.
Examples: age = 21, tokens = -5
float (Floating Point Number): This is used whenever you need to store numbers that contain
decimal points.
Examples: price = 99.99, pi = 3.14
str (String): This stores text data or a sequence of characters. In Python, you must always wrap
your strings inside single quotes ('...') or double quotes ("...").
Examples: country = "USA", grade = 'A'
bool (Boolean): This only holds two absolute values—either True (yes/correct) or False (no/
incorrect).
•
•
•
•
Page 1
pf2

Partial preview of the text

Download Python Variables & Data Types: Ultimate Student Revision Guide and more Lecture notes Computer Science in PDF only on Docsity!

CS101: PYTHON VARIABLES & DATA TYPES

Ultimate Student Revision & Quick Exam Prep Guide

1. Python Variables

In Python, a variable is basically like a container or a storage box where you can keep any data or value you want.

📦 Real-World Example (Kitchen Logic): Think of your kitchen at home. You probably have a jar labeled for "Sugar", and inside that jar, you keep sugar. Variables work exactly the same way in coding! We give a clear name to a box (the variable), and then we put a value inside it.

The Best Part About Python: Unlike other languages like C, C++, or Java, you don't need to manually declare or tell Python what kind of data you are going to store in the box ahead of time. Python is super smart—whatever value you put inside the variable, it automatically figures out the type and sets the box up perfectly.

x = 5 # 'x' is a box storing the number 5 name = "Ali" # 'name' is a box storing the text "Ali"

2. Data Types in Python

To store different kinds of information, Python majorly uses 4 basic built-in data types:

int (Integer): This stores pure whole numbers without any decimal points. It can be positive or negative. Examples: age = 21, tokens = - float (Floating Point Number): This is used whenever you need to store numbers that contain decimal points. Examples: price = 99.99, pi = 3. str (String): This stores text data or a sequence of characters. In Python, you must always wrap your strings inside single quotes ('...') or double quotes ("..."). Examples: country = "USA", grade = 'A' bool (Boolean): This only holds two absolute values—either True (yes/correct) or False (no/ incorrect).

Page 1

⚠️ CRITICAL EXAM TRICK:

In Python, the 'T' in True and 'F' in False must ALWAYS be capitalized. Writing 'true' or 'false' in lowercase will throw a straight-up Syntax Error! Professors love to test this quick trick in quizzes.

is_passed = True has_failed = False

3. Rules for Naming Variables

When you are creating a variable name in Python, you have to follow a few simple rules so your code doesn't crash:

Starting Character: A variable name must always start with a letter (A-Z, a-z) or an underscore (_). It can never start with a number (0-9). Case Sensitivity: Python is strictly case-sensitive. This means that words with different capitalization are treated as entirely separate variables. For example, age, Age, and AGE are three completely different boxes in memory! Allowed Symbols: You can only use alphanumeric characters and underscores (A-Z, 0-9, and _). Special characters like @, $, %, or spaces are strictly not allowed.

Page 2