Docsity

Python Variables, Data Types, and Operators: A Beginner's Guide, Study notes of Artificial Intelligence

Toggle sidebar
bg1
Python
Chapter 2
Variable & Data
Types
What Is a Variable?
A variable lets you store information and reuse it in your program.
๏ƒ˜like a container that stores data โ€” you give it a
name, and it holds a value that you can use or
change later
Example:
python
name = "Ayesha"
age = 20
print(name)
print(age)
Data Types in Python
data types define what kind of value a variable holds โ€”
like whether itโ€™s a number, text, list, or something else.
Basic Data Types in Python
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python Variables, Data Types, and Operators: A Beginner's Guide and more Study notes Artificial Intelligence in PDF only on Docsity!

Python

Chapter 2

Variable & Data

Types

What Is a Variable? A variable lets you store information and reuse it in your program. ๏ƒ˜ like a container that stores data โ€” you give it a name, and it holds a value that you can use or change later Example: python name = "Ayesha" age = 20 print(name) print(age) Data Types in Python data types define what kind of value a variable holds โ€” like whether itโ€™s a number, text, list, or something else. Basic Data Types in Python

Data Type Example Description int (^5) , -10 (^) Whole numbers float (^) 3.14, -0.5 Decimal numbers str "Hello" (^) Text (string) bool (^) True, False Logical values NoneType None (^) Represents "no value" What Is Dynamic Typing? ๏‚ท Python is a dynamically typed language ๏‚ท You donโ€™t need to declare the data type of a variable ๏‚ท Python figures it out automatically at runtime Rules for Naming Variables ๏‚ท Must start with a letter or underscore ( _ ) ๏‚ท Can contain letters, numbers, and underscores ๏‚ท Case-sensitive : Name and name are different ๏‚ท Should be descriptive (avoid x, y unless for math) How to check type of python? To check the data type of a value or variable in Python, you use the built-in function type()

2. Comparison Operators Used to compare values. Operator Meaning Example == (^) Equal to 5 == 5 โ†’ True != (^) Not equal to 5 != 3 โ†’ True > (^) Greater than 7 > 5 โ†’ True < (^) Less than 3 < 4 โ†’ True >= (^) Greater than or equal to 5 >= 5 โ†’ True <= (^) Less than or equal to 4 <= 6 โ†’ True 3. Logical Operators Used to combine conditional statements. Operator Meaning Example and (^) Both conditions true True and False โ†’ False or (^) At least one true True or False โ†’ True not (^) Inverts truth value not True โ†’ False 4. Assignment Operators Used to assign values to variables. Operator Meaning Example = (^) Assign x = 5 += (^) Add and assign x += 3 โ†’ x = x + 3 -= (^) Subtract and assign x -= 2 โ†’ x = x - 2

Operator Meaning Example *= (^) Multiply and assign x *= 4 /= (^) Divide and assign x /= 2 //= (^) Floor divide and assign x //= 2 %= (^) Modulus and assign x %= 3 **= (^) Exponent and assign x **= 2

5. Bitwise Operators Used for binary operations. Operator Meaning Example & (^) AND 5 & 3 โ†’ 1 OR 5 3 โ†’ 7 ^ (^) XOR 5 ^ 3 โ†’ 6 ~ (^) NOT ~5 โ†’ - << (^) Left shift 5 << 1 โ†’ 10 >> (^) Right shift 5 >> 1 โ†’ 2 6. Membership Operators Check if a value is in a sequence. Operator Meaning Example in (^) Value exists "a" in "apple" โ†’ True not in (^) Value doesnโ€™t exist "x" not in "apple" โ†’ True

7. Identity Operators

How input() Works? ๏‚ท It pauses the program and waits for the user to type something. ๏‚ท Whatever the user types is returned as a string. Example #input function name=input("Enter your name") print(name)

Important Notes

Example age=int(input("Enter your age")) print(type(age)) ๏‚ท The result of input() is always a string , even if the user types a number. ๏‚ท You need to convert it if you want another type (like int or float

Practice question

Q#

Write a program to add Two numbers?

Write a program to add Two Numbers

a= 20 b= 35 print("The sum of a and b:", a+b) #Output is:

Q#

Write a program to find reminder when a

number is divided by z

#write a program to find reminder when a number is divided by z a= 455 b= 4 print(a%b) #output is 3

Q#

Write a program to calculate square of a number entered by user #program to calculate square of a number entered by user a=int(input("Enter a NUmber:")) print(a*a)