Variables Constants Operators and Expressions
Variables Constants Operators and Expressions
variables are used to store data that can be referenced and manipulated during the
execution of a program. A variable essentially acts as a container that holds a value.
Each variable has a name and a data type (such as integer, string, float, etc.) that
determines what kind of data it can store.
Variable Declaration
Example in Python:
Common Variable Data Types:
Variable names must start with a letter (a-z, A-Z) or an underscore (_).
The rest of the variable name can include letters, digits (0-9), or underscores.
The name cannot be a keyword or reserved word (like if, class, return, etc.).
Variable names are case-sensitive (e.g., age and Age are different).
Variables are fundamental in almost all programming languages, and understanding
how to declare, assign values to, and use variables effectively is crucial for writing
functional code.
Constants in Programming
A constant is a type of variable whose value cannot be changed once it has been
assigned. Unlike regular variables, constants are meant to hold values that are intended
to remain the same throughout the execution of the program. The primary purpose of
constants is to make code more readable, maintainable, and avoid accidental changes
to important values.
1. Constants in Python
In Python, constants are not strictly enforced by the language itself. Python doesn't
have a special const keyword, but it is a convention to use all uppercase letters for
values that should not change.
Although Python doesn’t prevent modification of constants, using uppercase letters
helps indicate that they are intended to be constants and should not be changed.
Operators in Programming
Types of Operators
Operators in Programming
Types of Operators
1. Arithmetic Operators
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
% : Modulo (remainder of division)
2. Relational (Comparison) Operators
These operators compare two values and return a boolean result (True or False).
== : Equal to
!= : Not equal to
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Less than or equal to
Operators in Programming
Types of Operators
1. Arithmetic Operators
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
% : Modulo (remainder of division)
5. Bitwise Operators
& : AND
| : OR
^ : XOR (exclusive OR)
~ : NOT (complement)
<< : Left shift
>> : Right shift
6. Unary Operators
Types of Expressions:
1. Arithmetic Expressions
These are expressions that use arithmetic operators like +, -, *, /, and % to perform
mathematical operations.
Example in Python:
Expressions in Programming
Types of Expressions:
1. Arithmetic Expressions
These are expressions that use arithmetic operators like +, -, *, /, and % to perform
mathematical operations.
Example in Python:
python
Copy code
a = 5
b = 3
print(result)
Example in C:
c
Copy code
#include <stdio.h>
int main() {
int a = 5, b = 3;
// Arithmetic expression
int result = a + b * 2; // result = 5 + (3 * 2) = 11
printf("%d\n", result);
return 0;
}
2. Relational (Comparison) Expressions
These expressions evaluate a condition and return True or False based on relational
operators like ==, !=, >, <, etc.
3. Logical Expressions
Logical expressions combine boolean values using logical operators like and, or, not
(or &&, ||, ! in many languages).
4. Conditional (Ternary) Expressions
These involve using the assignment operator (=, +=, -=, etc.) to assign a value to a
variable.
6. Unary Expressions
Unary expressions use a single operand to perform operations such as increment (++),
decrement (--), negation (-), or logical NOT (!).
7. Bitwise Expressions
Bitwise expressions operate on the individual bits of integer data types, using bitwise
operators like &, |, ^, <<, and >>.
Summary
Arithmetic (e.g., a + b * c)
Relational (e.g., a == b)
Logical (e.g., a && b)
Conditional (Ternary) (e.g., x ? y : z)
Assignment (e.g., a = b)
Unary (e.g., -x)
Bitwise (e.g., a & b)
Understanding expressions is crucial for writing efficient, readable, and functional code
in any programming language.