VARIABLES IN PYTHON
แ แแฃแญ แ Python แตแ variables แแแแต แฅแแแซแแแข
Stay Tuned!
@python_pioneers
@python_pioneers
แ แแฃแญ แ Python แตแ variables แแแแต แฅแแแซแแแข
Stay Tuned!
@python_pioneers
@python_pioneers
๐3
VARIABLES IN PYTHON
In Python, Variables data values store แแแจแ แแ แจแแซแแแแตแข แ Mathematics แซแจแ a variable is a symbol that represents a numberแก แ แฅแแแแ แแ แฐแแ แแฐแ แแแข แ แฐแแณแณแญแก variable แ python แแแ data value แแ แชแซ แแแก it can be a single letter or a word.
1. Variable Declaration: You declare variables in Python simply by assigning a value to them using the
It is like แจแแ แณแฅแ แ แแฝแ แฅแแ แแฃ แจแ แตแ แตแฐแกแณแแฝแแฃ แฅแแ แจแแแแฝแแตแ แแแญ แณแฅแ แแตแฅ แแจแ แตแฝแแแฝแ so think the same way about variables.
2. Variable Naming Rules:
-แจvariable แฅแ แแฐแแฃ แแฅแญ แฅแ underscore(_) แแซแ แญแฝแแแข
- แจvariable แฅแ แ แแฅแญ แแแแญ แ แญแปแแแข
- Variable names case-sensitive แแธแแข แแแตแ แแแณแ age แฅแ Age แ แแต แ แญแแต variable แ แญแฐแแแข
3. Data Types: Python is dynamically typed, แแแตแ แจvariableแกแ data type declare แแจแ แ แญแ แ แ แฅแแแข Python automatically แจแแแฃแแ data แ แแจแต แจvariableแกแ data type assign แซแจแแแฃ แญแ แแ แแแแ programming languages แจแแฐแซ แ แญแฐแแแข
แแแณแแก difference in variable declaration between Java and Python แฅแแญแก
Declaring a variable in Java
แ แแ แแ variable declare แตแแจแ data typeแกแแ assign แแจแ แญแแญแฅแแแข แแ we don't need to do this in python, since it automatically assigns the type of data at the instant we give it a value.
Declaring a variable in Python:
Let's assign different data types to a variable in python
4. Variable Reassignment: You can reassign a variable to a different value. แฐแแณแณแญ variable แ แแ แ แ value reassign แแจแ แฅแแฝแแแแฃ that means we can give it another value.
...แญแแฅแแ
Practice by assigning different values for variables.
โDon't forget to invite your friendsโ
@python_pioneers
@python_pioneers
In Python, Variables data values store แแแจแ แแ แจแแซแแแแตแข แ Mathematics แซแจแ a variable is a symbol that represents a numberแก แ แฅแแแแ แแ แฐแแ แแฐแ แแแข แ แฐแแณแณแญแก variable แ python แแแ data value แแ แชแซ แแแก it can be a single letter or a word.
1. Variable Declaration: You declare variables in Python simply by assigning a value to them using the
= sign. Variable declare แตแแจแ แ computerแก memory แแตแฅ แจแแแแแแแ value store แจแแแจแแ แต space แญแแ แซแแข It is like แจแแ แณแฅแ แ แแฝแ แฅแแ แแฃ แจแ แตแ แตแฐแกแณแแฝแแฃ แฅแแ แจแแแแฝแแตแ แแแญ แณแฅแ แแตแฅ แแจแ แตแฝแแแฝแ so think the same way about variables.
x = 5
2. Variable Naming Rules:
-แจvariable แฅแ แแฐแแฃ แแฅแญ แฅแ underscore(_) แแซแ แญแฝแแแข
- แจvariable แฅแ แ แแฅแญ แแแแญ แ แญแปแแแข
- Variable names case-sensitive แแธแแข แแแตแ แแแณแ age แฅแ Age แ แแต แ แญแแต variable แ แญแฐแแแข
my_variable = "Hello"
3. Data Types: Python is dynamically typed, แแแตแ แจvariableแกแ data type declare แแจแ แ แญแ แ แ แฅแแแข Python automatically แจแแแฃแแ data แ แแจแต แจvariableแกแ data type assign แซแจแแแฃ แญแ แแ แแแแ programming languages แจแแฐแซ แ แญแฐแแแข
แแแณแแก difference in variable declaration between Java and Python แฅแแญแก
Declaring a variable in Java
int x = 5;
แ แแ แแ variable declare แตแแจแ data typeแกแแ assign แแจแ แญแแญแฅแแแข แแ we don't need to do this in python, since it automatically assigns the type of data at the instant we give it a value.
Declaring a variable in Python:
x = 5
Let's assign different data types to a variable in python
x = 5 # x is an integer
y = 3.14 # y is a float
z = "Hello" # z is a string
4. Variable Reassignment: You can reassign a variable to a different value. แฐแแณแณแญ variable แ แแ แ แ value reassign แแจแ แฅแแฝแแแแฃ that means we can give it another value.
x = 5
x = 10 # x is now assigned the value 10
# x is no more 5
...แญแแฅแแ
Practice by assigning different values for variables.
โDon't forget to invite your friendsโ
@python_pioneers
@python_pioneers
๐5๐2
Multiple-choice questions pertaining to variable usage in Python.
โTest your knowledge about variables with the following questions โ
โTest your knowledge about variables with the following questions โ
1. What is a variable in Python?
Anonymous Quiz
7%
a) A constant value
7%
b) A reserved keyword
77%
c) A name that refers to a value
9%
d) A function
2. Which of the following variable names is invalid in Python?
Anonymous Quiz
16%
a) my_variable
63%
b) 2nd_variable
11%
c) variable2
11%
d) _variable
3. What is the correct way to assign a value of 5 to a variable named "x" in Python?
Anonymous Quiz
87%
a) x = 5
3%
b) 5 = x
10%
c) x == 5
0%
d) x := 5
4. What happens if you try to access a variable that has not been defined in Python?
Anonymous Quiz
30%
a) It raises a SyntaxError
15%
b) It creates a new variable with that name
38%
c) It raises a NameError
17%
d) It returns None
๐2
5. In Python, is it possible to assign multiple variables in a single line?
Anonymous Quiz
74%
a) Yes, using comma-separated values
11%
b) No, Python does not support multiple variable assignment
10%
c) Yes, but only for integers
4%
d) Yes, but only for strings
๐2
OPERATIONS IN PYTHON
Here are some of the fundamental operations in python. Mathematics แแญ แซแจแ there are operations like addition , subtraction... แฅแแแ แ operations Arithmetic operations แฅแแแธแแแแฃ Python แแ Arithmetic operations แแญ แจแฐแแฐแ แ แญแฐแแแฃ There are lots of operations.
Operands: Operands are the values or variables on which an operator acts. In other words, operands are the data that are manipulated by an operator in an expression.
แแแณแแก In the expression 5 + 3, the _operands_ are the numbers 5 and 3, while the plus sign (+) is the _operator_ that acts on the _operands_ to perform the addition operation.
แ Python แฐแแ operands variables, constants, แแญแ expressions แแแ แญแฝแแแข
1. Arithmetic Operations:
- Addition (+): แแแต แแฅแฎแฝแ แแแฐแแญ แจแแแ แแแ type of operation แแแข.
- Subtraction (-): แจแแแแชแซแ แแฅแญ แแแฐแแแ แแแแแต แจแแซแแแแ operation แแแข
- Multiplication (*): แญแ แแ แฐแแ แแแต operands แแแฃแแตแก
- Division (/): แจแแแแชแซแแ แแฅแญ แ แแแฐแแ แแฅแญ แแแซแแ แซแแแแแ(The result is always a float).
- Modulus (%): Returns the remainder of the division(แแช return แซแจแแ)
- Exponentiation (**): Raises the first operand to the power of the second.
x**y แแแต x to the power of y แฅแแฐ แแแต แแแข
- Floor Division (//): Returns the quotient without the remainder(แญแแแแญ แซแ แแช return แซแจแแ)
Code Example:
2. Comparison Operations:
แฅแแแ operations แฐแแ แแแต values(operands) compare แ แแตแจแ boolean value return แซแจแแ (True or False) based on on the comparison result.
- Equal (==): Checks if two operands are equal.
- Not Equal (!=): Checks if two operands are not equal.
- Greater Than (>): Checks if the first operand is greater than the second.
- Less Than (<): Checks if the first operand is less than the second.
- Greater Than or Equal To (>=): Checks if the first operand is greater than or equal to the second.
- Less Than or Equal To (<=): Checks if the first operand is less than or equal to the second.
Code Example:
...แญแแฅแแ
@python_pioneers
@python_pioneers
Here are some of the fundamental operations in python. Mathematics แแญ แซแจแ there are operations like addition , subtraction... แฅแแแ แ operations Arithmetic operations แฅแแแธแแแแฃ Python แแ Arithmetic operations แแญ แจแฐแแฐแ แ แญแฐแแแฃ There are lots of operations.
Operands: Operands are the values or variables on which an operator acts. In other words, operands are the data that are manipulated by an operator in an expression.
แแแณแแก In the expression 5 + 3, the _operands_ are the numbers 5 and 3, while the plus sign (+) is the _operator_ that acts on the _operands_ to perform the addition operation.
แ Python แฐแแ operands variables, constants, แแญแ expressions แแแ แญแฝแแแข
1. Arithmetic Operations:
- Addition (+): แแแต แแฅแฎแฝแ แแแฐแแญ แจแแแ แแแ type of operation แแแข.
- Subtraction (-): แจแแแแชแซแ แแฅแญ แแแฐแแแ แแแแแต แจแแซแแแแ operation แแแข
- Multiplication (*): แญแ แแ แฐแแ แแแต operands แแแฃแแตแก
- Division (/): แจแแแแชแซแแ แแฅแญ แ แแแฐแแ แแฅแญ แแแซแแ แซแแแแแ(The result is always a float).
- Modulus (%): Returns the remainder of the division(แแช return แซแจแแ)
- Exponentiation (**): Raises the first operand to the power of the second.
x**y แแแต x to the power of y แฅแแฐ แแแต แแแข
- Floor Division (//): Returns the quotient without the remainder(แญแแแแญ แซแ แแช return แซแจแแ)
Code Example:
a = 10
b = 5
addition = a + b # result = 15
subtraction = a - b # result = 5
multiplication = a * b # result = 50
division = a / b # result = 2.0
modulus = a % b # result = 0 since the rem is 0
exponentiation = a ** b # result = 100000
floor_division = a // b # result = 2
2. Comparison Operations:
แฅแแแ operations แฐแแ แแแต values(operands) compare แ แแตแจแ boolean value return แซแจแแ (True or False) based on on the comparison result.
- Equal (==): Checks if two operands are equal.
- Not Equal (!=): Checks if two operands are not equal.
- Greater Than (>): Checks if the first operand is greater than the second.
- Less Than (<): Checks if the first operand is less than the second.
- Greater Than or Equal To (>=): Checks if the first operand is greater than or equal to the second.
- Less Than or Equal To (<=): Checks if the first operand is less than or equal to the second.
Code Example:
x = 10
y = 5
equal = x == y # result = False since 10 != 5
not_equal = x != y # result = True since 10 != 5
greater_than = x > y # result = True since 10 > 5
less_than = x < y # result = False since 10 > 5
greater_than_or_equal_to = x >= y # result = True
less_than_or_equal_to = x <= y # result = False
...แญแแฅแแ
@python_pioneers
@python_pioneers
๐4๐2
...contd
3. Logical Operations:
The operands here are boolean values (True and False).
- AND (and): Returns True if both operands are true.
- OR (or): Returns True if any of the operands are true.
- NOT (not): Returns True if the operand is false.
Code Example:
4. Assignment Operations:
แฅแแแ แฐแแ แ แแตแ value แvariable assign แแแตแจแ แแ แจแแแ แแแแข
+= : แ แแ แ แฉแ แซแแแ operand แ แแซ แ แฉแ แฃแแ operand แแญ แญแฐแแญแ แแคแฑแ แ แแซ แ แฉแ แแแ operand assign แซแจแแแแข
-= : แ แแ แ แฉแ แซแแแ operand แจแแซแ แ แแแแต แแคแฑแ แ แแซ แ แฉแ แแแ operand assign แซแจแแแข
*= : แ แแซ แ แฉแ แซแแแ operand แ แแ แ แฉแ แฃแแ แซแฃแแ แแคแฑแ แ แแซ แ แฉแ แแแ operand assign แซแจแแแข
/= : แ แแซ แ แฉแ แซแแแ operand แ แแ แ แฉแ แฃแแ operand แซแซแแแ แแคแฑแ แ แแซ แ แฉแ แแแ operand assign แซแจแแแแข
Code Example:
These are just some of the basic operations in python, we believe you have grasped some knowledge about them.
@python_pioneers
@python_pioneers
3. Logical Operations:
The operands here are boolean values (True and False).
- AND (and): Returns True if both operands are true.
- OR (or): Returns True if any of the operands are true.
- NOT (not): Returns True if the operand is false.
Code Example:
p = True
q = False
and_result = p and q # result = False
or_result = p or q # result = True
not_result = not p # result = False
4. Assignment Operations:
แฅแแแ แฐแแ แ แแตแ value แvariable assign แแแตแจแ แแ แจแแแ แแแแข
+= : แ แแ แ แฉแ แซแแแ operand แ แแซ แ แฉแ แฃแแ operand แแญ แญแฐแแญแ แแคแฑแ แ แแซ แ แฉแ แแแ operand assign แซแจแแแแข
-= : แ แแ แ แฉแ แซแแแ operand แจแแซแ แ แแแแต แแคแฑแ แ แแซ แ แฉแ แแแ operand assign แซแจแแแข
*= : แ แแซ แ แฉแ แซแแแ operand แ แแ แ แฉแ แฃแแ แซแฃแแ แแคแฑแ แ แแซ แ แฉแ แแแ operand assign แซแจแแแข
/= : แ แแซ แ แฉแ แซแแแ operand แ แแ แ แฉแ แฃแแ operand แซแซแแแ แแคแฑแ แ แแซ แ แฉแ แแแ operand assign แซแจแแแแข
Code Example:
x = 5
x += 3 # Equivalent to x = x + 3
x -= 2 # Equivalent to x = x - 2
x *= 4 # Equivalent to x = x * 4
x /= 2 # Equivalent to x = x / 2
These are just some of the basic operations in python, we believe you have grasped some knowledge about them.
@python_pioneers
@python_pioneers
๐3โค1
GROW THROUGH WHAT YOU GO THROUGH.
แจแแซแแฅแแ แแแแแ แแฐแ แฅแ แแฐแแญแ แแแแญแฃ แแแฐแ แฅแ แแแ แแจแญ แฅแตแ แญแฐแ แแแข แ แแซแแฅแแ แฝแแฎแฝ แจแแแจแญ แแญแ แจแแแฐแต แญแแ แself development แฅแ แฅแซแณแฝแแ แแแปแปแ แฅแแฐ แฅแตแ แ แตแญแแ แแแ แ แ แแฅแแข
แแแซแ แแฝแต!
@python_pioneers
แจแแซแแฅแแ แแแแแ แแฐแ แฅแ แแฐแแญแ แแแแญแฃ แแแฐแ แฅแ แแแ แแจแญ แฅแตแ แญแฐแ แแแข แ แแซแแฅแแ แฝแแฎแฝ แจแแแจแญ แแญแ แจแแแฐแต แญแแ แself development แฅแ แฅแซแณแฝแแ แแแปแปแ แฅแแฐ แฅแตแ แ แตแญแแ แแแ แ แ แแฅแแข
แแแซแ แแฝแต!
@python_pioneers
๐5๐ฅฐ2
Python Pioneers: A Beginner's Guide ๐ช๐น
COMMENTS IN PYTHON Pythonแ แ แ แแญแ แฅแแแญ! Stay Tuned! @python_pioneers
COMMENTS IN PYTHON
แ Pythonแก Comments แ Python interpreter ignore แจแแฐแจแ แแฅแซแชแซแแฝ แแธแแข แจแแแแแ code purpose แแแแแ แฃ แฅแแดแต แฅแแฐแแฐแซแฃ แฅแ แแแฝ แแจแแแฝแ แแแแ แฅแแ แแแธแแแแข Comments are crucial for code readability and maintenance
TYPES OF COMMENTS IN PYTHONแก-
1. Single-Line Commentsแก แฅแแแ comments แ
2. Multi-Line Commentsแก แ Python แฃ แMulti-line comments specific syntax แจแแแข แแแญ แแ แ แฅแแแแ แแ multi-line comments แแแแ แญ triple qoutes(แถแตแต แตแแ แญแฐ แฅแ แต)(''' แแญแ """) แแ แแ แฅแแฝแแแแข
3. Docstringsแก Docstringsแก functions, modules, แฅแ classes document แแแตแจแ แญแ แ แแแข They are enclosed in triple quotes and provide more detailed explanations of the purpose, parameters, and return values of functions.
แแแณแ แญแแแ code แฅแแแแจแตแก
แฅแแ in general comments in any programming languages แจแแซแแแแแต แจแแแแ code แ แแ แแญแแต แแแแต แฅแแฐแแฐแซ แแแแแ แแแฃ แจแแแแแ code แฐแแ แแ แฐแ แฅแแฒแซแจแ share แจแแแจแ แจแแ แจแแซแแ แ แฐแ แจแ แแ แตแ programแก แ แฐแซแญ แฅแแฒแจแณ แซแฐแญแแแข
@python_pioneers
แ Pythonแก Comments แ Python interpreter ignore แจแแฐแจแ แแฅแซแชแซแแฝ แแธแแข แจแแแแแ code purpose แแแแแ แฃ แฅแแดแต แฅแแฐแแฐแซแฃ แฅแ แแแฝ แแจแแแฝแ แแแแ แฅแแ แแแธแแแแข Comments are crucial for code readability and maintenance
TYPES OF COMMENTS IN PYTHONแก-
1. Single-Line Commentsแก แฅแแแ comments แ
# แแแญแต แญแแแซแ แฅแ แฅแตแจ line แแจแจแป แตแจแต แญแแฅแแ แข แฅแแแ
แ แฐแแ แแ แซแญแญ แแฅแซแชแซแแฝ แฅแแ แแแธแแแแข# This is a single-line comment
x = 5 # This is also a single-line comment
2. Multi-Line Commentsแก แ Python แฃ แMulti-line comments specific syntax แจแแแข แแแญ แแ แ แฅแแแแ แแ multi-line comments แแแแ แญ triple qoutes(แถแตแต แตแแ แญแฐ แฅแ แต)(''' แแญแ """) แแ แแ แฅแแฝแแแแข
'''
This is a multi-line comment.
It spans across multiple lines.
'''
3. Docstringsแก Docstringsแก functions, modules, แฅแ classes document แแแตแจแ แญแ แ แแแข They are enclosed in triple quotes and provide more detailed explanations of the purpose, parameters, and return values of functions.
แแแณแ แญแแแ code แฅแแแแจแตแก
def add_numbers(a, b):
"""
This function adds two numbers together.
Parameters:
a (int): The first number.
b (int): The second number.
Returns:
int: The sum of a and b.
"""
return a + b
แฅแแ in general comments in any programming languages แจแแซแแแแแต แจแแแแ code แ แแ แแญแแต แแแแต แฅแแฐแแฐแซ แแแแแ แแแฃ แจแแแแแ code แฐแแ แแ แฐแ แฅแแฒแซแจแ share แจแแแจแ แจแแ แจแแซแแ แ แฐแ แจแ แแ แตแ programแก แ แฐแซแญ แฅแแฒแจแณ แซแฐแญแแแข
@python_pioneers
๐3
There is no straight path to success. The problems we face have the ability to shape us. Grow through them.
HARD WORK
|
|
PROBLEM
|
SETBACK
|
FAILURE
|
CHALLENGE
|
OBSTACLE
|
|
SUCCESS
Have a nice day!
@python_pioneers
HARD WORK
|
|
PROBLEM
|
SETBACK
|
FAILURE
|
CHALLENGE
|
OBSTACLE
|
|
SUCCESS
Have a nice day!
@python_pioneers
๐1
Let's Revise what we have been learning with these questions.
๐๐๐๐๐๐๐๐๐
๐๐๐๐๐๐๐๐๐
1. What is the correct way to declare a variable in Python?
Anonymous Quiz
7%
A. var x = 5
67%
B. x = 5
25%
C. int x = 5
2%
D. declare x as 5
2. What is the purpose of comments in Python code?
Anonymous Quiz
6%
A. To make the code run faster
85%
B. To explain the purpose of the code
6%
C. To hide code from other programmers
2%
D. To declare variables
3. What type of operations can you perform on variables in Python?
Anonymous Quiz
12%
A. Mathematical operations
6%
B. String operations
0%
C. Logical operations
82%
D. All of the above
4. What is the difference between declaring a variable and assigning a value to it in Python?
Anonymous Quiz
19%
A. There is no difference
47%
B. Declaring initializes with a value, assigning a value changes the value of an existing variable
30%
C. Declaring a variable creates a variable, while assigning a value creates a new variable
4%
D. Assigning a value is only done with strings
5. What is the significance of using an underscore (_) in variable names in Python?
Anonymous Quiz
22%
A. It indicates a reserved word
66%
B. It separates words in a variable name
6%
C. It is not allowed in variable names
6%
D. It is used for mathematical operations
๐1