[go: up one dir, main page]

0% found this document useful (0 votes)
9 views9 pages

Python

The document provides a comprehensive overview of three key concepts in Python: data types, variables, and operators. It details various data types including primitive, collection, and special types, along with their uses and examples. Additionally, it covers variable naming rules, creation, type checking, type conversion, and different types of operators such as arithmetic, assignment, comparison, logical, membership, and identity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Python

The document provides a comprehensive overview of three key concepts in Python: data types, variables, and operators. It details various data types including primitive, collection, and special types, along with their uses and examples. Additionally, it covers variable naming rules, creation, type checking, type conversion, and different types of operators such as arithmetic, assignment, comparison, logical, membership, and identity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PYTHON

Great, Dhruv! Let’s now fully break down the 3 key


concepts in Python:

✅ 1. DATA TYPES IN PYTHON

🔷 What is a Data Type?


A data type defines the kind of value a variable can
store.
Python automatically assigns the correct data type
when you assign a value — this is called dynamic
typing.

🔷 Types of Data Types in Python


Python has two main categories of data types:
🔹 A. Primitive / Basic Data Types
Data
Description Common Uses
Type
Whole numbers Counting, IDs, loops,
int
(positive or negative) indexing
Calculations,
float Decimal numbers measurement, scientific
data
Boolean value (True Conditions, flags, logic
bool
or False) handling
Sequence of Names, messages, file
str
characters (text) paths
Examples:
x = 10 # int
pi = 3.14 # float
is_on = True # bool
name = "Dhruv" # str

🔹 B. Collection / Compound Data Types

Data
Description Common Uses
Type
Ordered, mutable Storing multiple items
list
sequence (e.g., scores, names)
Ordered, immutable Fixed data like
tuple
sequence coordinates, settings
set Unordered, unique Removing duplicates,
Data
Description Common Uses
Type
items membership testing
Storing structured data
dict Key-value pairs
(e.g., user profiles)
Examples:
fruits = ["apple", "banana", "cherry"] # list
point = (10, 20) # tuple
unique_nums = {1, 2, 3, 3} # set => {1, 2, 3}
student = {"name": "Dhruv", "age": 21} # dict

🔹 C. Special Data Types

Data Examp
Description Use Case
Type le
NoneTyp Represents x= Function defaults,
e absence of value None placeholders
Complex numbers Scientific
z=2+
complex with real + computing,
3j
imaginary parts electrical systems

✅ Summary: Use Cases of Data Types

Examp
Type Used For
le
int 10 Loop counters, IDs
Examp
Type Used For
le
float 3.14 Prices, averages
bool True Logic conditions
User names, file
str "Hello"
paths
Collections of
list [1,2,3]
items
tuple (1,2) Fixed pairs
dict {"x":1} Structured records
set {1,2} Unique values
NoneTyp
None Missing values
e
complex 2+3j Math/science use

✅ 2. VARIABLES IN PYTHON

🔷 What is a Variable?
A variable is a name that refers to a value stored in
memory.
You can think of it as a label attached to a value.

🔷 Rules for Variable Naming


✅ Allowed:
 Letters, numbers, underscores
 Cannot start with a number
 Case-sensitive (Name ≠ name)
🚫 Not Allowed:
 Spaces
 Reserved keywords like for, class, if

🔷 Creating Variables
x = 10
name = "Dhruv"
price = 99.99
is_valid = True

🔷 Multiple Assignment
a, b, c = 1, 2, 3
x = y = z = 100

🔷 Type Checking
Use type() function to know the data type of a variable:
print(type(name)) # <class 'str'>

🔷 Type Conversion (Casting)


You can change the data type using casting functions:
int("10") # 10
float("3.5") # 3.5
str(123) # "123"
bool(0) # False

✅ 3. OPERATORS IN PYTHON

🔷 What is an Operator?
Operators are symbols that perform operations on
variables and values.

🔷 Types of Operators

✅ A. Arithmetic Operators

Operat Examp Outp


Description
or le ut
+ Addition 5+3 8
- Subtraction 10 - 4 6
* Multiplication 2*3 6
/ Division (float) 5/2 2.5
// Floor division 5 // 2 2
Modulus
% 5%2 1
(remainder)
** Exponentiation 2 ** 3 8
✅ B. Assignment Operators

Operator Example Meaning


= x=5 Assign value
+= x += 2 x=x+2
-= x -= 1 x=x-1
*= x *= 3 x=x*3
/= x /= 2 x=x/2
//=, %=, same as with those
**= above operators

✅ C. Comparison Operators
Used to compare two values.

Operat Examp Resu


or le lt
== 5 == 5 True
!= 5 != 3 True
> 5>3 True
< 3<5 True
>= 5 >= 5 True
<= 4 <= 3 False

✅ D. Logical Operators
Used with boolean values (True, False).

Operat Resu
Description Example
or lt
True and
and True if both are True False
False
True if at least one is True or
or True
True False
not Reverses the value not True False

✅ E. Membership Operators
Used to test if a value is in a sequence.

Operat Resu
Example
or lt
in "a" in "apple" True
"x" not in
not in True
"apple"

✅ F. Identity Operators
Compare memory location of two variables.

Operat Examp
Result
or le
True if x and y refer to same
is x is y
object
is not x is not True if they don’t refer to same
Operat Examp
Result
or le
y object

🎯 Summary Table

Concept Details
Data int, float, str, bool, list, tuple, set, dict, None,
Types complex
Variables Names that store data values
Operator Arithmetic, Assignment, Comparison, Logical,
s Membership, Identity

Would you like a PDF version of this for quick revision


or want to move on to the next topic (like control
structures, loops, or functions)?

You might also like