- ALU: Handles arithmetic and logical operations.
- CU: Coordinates tasks.
-
On behalf of Accessibility by CPU
- Primary Memory: Directly accessible to CPU
e.g., RAM, ROM, Cache - Secondary Memory: Not directly accessible to CPU
e.g., HDD, SSD, Pen Drive, CD, DVD, Blu-ray disk, Memory card
- Primary Memory: Directly accessible to CPU
-
On behalf of Content Retention Capacity w.r.t. Power Supply
- Volatile Memory: Content is lost when power is off
e.g., RAM - Non-Volatile Memory: Content remains regardless of power supply
e.g., HDD, SSD, Pen Drive, ROM
- Volatile Memory: Content is lost when power is off
CPU <-------> Primary Storage <-----> Secondary Storage
(Process) (Program)
In Python, we write code (source code) using a high-level programming language. Ultimately, the computer understands only binary, i.e., machine code.
source code -----> Interpreter -----> Binary
- Converts source code into machine code line by line, executing it as it goes.
- Stops immediately if an error exists, prompting the user to correct it.
- The generated machine code is one-time use; it must be re-interpreted for each execution.
- Requires more memory for execution since the interpreter must always be in memory.
- Generally slower than compilers.
- More secure than compilers, as it performs checks for every execution.
Examples: Python, Java
In contrast, for languages like C or C++:
CPP ------> Compiler -------> Binary
- Converts source code into machine code all at once.
- Reports all errors in one go.
- The generated machine code is stored in HDD, allowing multiple executions without recompilation.
- Does not need to be in memory for execution after compilation, making it memory efficient.
- Generally faster than interpreters.
- Less secure since it does not check security for every execution.
Examples: C, C++
- 4 GB = 4 x 1024 MB
- 4 GB = 4 x 1024 x 1024 KB
- 4 GB = 4 x 1024 x 1024 x 1024 B
- 4 GB = 2² x 2¹⁰ x 2¹⁰ x 2¹⁰ B
- 4 GB = 2³² B
Every memory location (1 byte) is identified using a physical address, which is hard for humans to remember (e.g., 0xAFE2:585A). Instead, we use variable names to identify memory locations in a more human-friendly way.
# Example of variable naming
user_age = 25 # This is a human-friendly name for a memory location
Using variable names makes it easier to manage and understand our code!
A variable is a named memory location whose value can be changed.
In Python, a variable is a reference to a memory location (called an object) that holds the value. This means that a variable does not store the value directly; instead, it stores the location of the object where the value is actually stored.
variable_name = value
- Can consist of a combination of A-Z, a-z, 0-9, and the special symbol
_
. - Cannot start with a digit.
- Cannot contain commas or spaces.
- Must not be a reserved keyword.
- Convention: Variable names should be meaningful.
-
camelCaseNamingConvention: Each word starts with a capital letter, and there are no spaces or underscores.
- Example:
basicSalary
,userAge
- Example:
-
snake_case_naming_convention: Words are all lowercase and separated by underscores.
- Example:
basic_salary
,user_age
- Example:
a
abc
basic_salary
gold9
simple123_321
basicSalary
(CamelCase)userAge
(CamelCase)
9gold
(cannot start with a digit)basic salary
(cannot contain space)basic,salary
(cannot contain comma)if
,elif
,def
(reserved keywords)123*123
(cannot contain special symbols other than_
)
In Python programming, the type()
function is used to check the data type of a variable.
age = 17
print(age, type(age))
percentage = 85.25
print(percentage, type(percentage))
st_name = "Om"
print(st_name, type(st_name))
gender = 'M'
print(gender, type(gender))
stack heap
|74859658|--->|___17___| (object)
age 74859658
|74253698|--->|__85.25_| (object)
percentage 74253698
|74147859|--->|__Om_| (object)
st_name 74147859
|74268952|--->|__M_| (object)
gender 74268952
17 <class 'int'>
85.25 <class 'float'>
Om <class 'str'>
M <class 'str'>
During execution, every process occupies some space in the RAM called address space. The address has the following parts:
-
Data Segment: Contains values and variables. It includes:
- Stack:
- Memory can be given a human-friendly name.
- Memory allocation is fast and cannot be resized (static).
- Heap:
- Memory cannot be named.
- Memory allocation is slower and can be resized (dynamic).
- Stack:
-
Code Segment: Contains functions and statements.
The print()
function is used to output text to the terminal.
print(argument, sep=' ', end='\n') # \n is for a new line
- The
end
parameter specifies what to print after the statement completes. - The
sep
parameter specifies the default separator when multiple values are printed.
print("Masai")
print("School")
print("Masai", "School")
Masai
School
Masai School
You can change the values for sep
and end
parameters:
print("Masai School", "Bengaluru", sep=', ')
print("Masai", end='-')
print("A tribe that survives by skills")
print("Masai", "Bengaluru", "Karnataka", sep=' : ', end=' # ')
print("859674")
Masai School, Bengaluru
Masai-A tribe that survives by skills
Masai : Bengaluru : Karnataka # 859674
name = 'Anuj'
age = 18
state = 'Gujarat'
print(name, "of age", age, "from", state, "will vote for the first time.")
print("name of age age from state will vote for first time.")
Anuj of age 18 from Gujarat will vote for the first time.
name of age age from state will vote for first time.
Typecasting is converting a value from one data type to another.
"1"
is a string value.1
is an integer.1.0
is a float value.
str(parameter)
: Converts parameter to string value.int(parameter)
: Converts parameter to integer value.float(parameter)
: Converts parameter to float value.
a = 1
b = a
c = str(b) # converting int to str
d = float(a) # converting int to float
print(a, type(a)) # 1, <class 'int'>
print(b, type(b)) # 1, <class 'int'>
print(c, type(c)) # '1', <class 'str'>
print(d, type(d)) # 1.0, <class 'float'>
print() # just to insert a new line in the output
e = 1.5
f = int(e)
g = str(e)
print(e, type(e)) # 1.5, <class 'float'>
print(f, type(f)) # 1, <class 'int'>
print(g, type(g)) # '1.5', <class 'str'>
Comments are written for human understanding; machines do not read them. In Python, comments start with #
.
# This is a single line comment
# This is
# a multi-line
# comment
A data type defines:
- (i) The range/set of values for a variable.
- (ii) The operations that can be performed on a variable.
-
Number:
int
: Non-fractional numbers.float
: Fractional numbers.boolean
: Only two values:True
orFalse
.complex
: Not covered here.
-
Sequence:
string
: Anything inside''
or""
.list
: Discussed later.tuple
: Discussed later.
-
Set:
set
: Discussed later.
-
None:
None
: Discussed later.
-
Mapping:
Dictionary
: Discussed later.
# Print statement for Kid's Education and "Good Morning"
print("Kid's education")
print('"Good Morning"')
- Operator: A symbol that represents an operation (e.g.,
+
,~
,<
,>
). - Operand: A variable or value on which the operation is applied.
- Expression: A valid combination of operators and operands.
a = 10 + 20 # This is an expression
# Operators: +, =
# Operands: for +; 10 & 20 are operands, for =; a & 30 are operands.
-
Based on the Number of Operands:
- Unary Operator: Applicable on one operand.
a = -10 # Here, - is unary
- Binary Operator: Applicable on two operands.
a = 10 - 5 # Here, - is binary
- Ternary Operator: Applicable on three operands (discussed later).
- Unary Operator: Applicable on one operand.
-
Based on the Nature of Operations:
- Arithmetic operators
- Relational operators
- Logical operators
Arithmetic operators are used for basic arithmetic operations:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)//
(Floor Division)%
(Modulus)**
(Exponential)
print(10 + 5) # 15
print(10 - 3) # 7
print(10 * 3) # 30
print(10 / 3) # 3.33333333
# Floor Division
print(10 // 3) # 3 (quotient only)
# Modulus
print(10 % 3) # 1
print(10 % 2) # 0
print(10 % 5) # 0
print(10 % 1) # 0
print(10 % 10) # 0
# Exponential
print(2 ** 3) # 8
print(36 ** 0.5) # 6
print(3 ** 3) # 27
Operators are evaluated based on their priority, and the order of precedence is:
()
(Parentheses)**
(Exponential)*
,/
,//
,%
(Multiplication/Division)+
,-
(Addition/Subtraction)
For operators with the same priority, their execution is based on associativity:
- Right to left:
**, =
- Left to right:
*, /, //, %, +, -
print(2 ** 3 // 5 + 4) # Output: 5
print(10 * 3 // 4) # Output: 7
print(2 ** 4 * 8 + 24 + (4 // 3)) # Output: 153
print(3 * 6 / 5) # Output: 3.6
print(3 * 6 // 5) # Output: 3
print(3 // 6 * 5) # Output: 0
print(2 ** 3 ** 2) # Output: 512
Relational operators are used for comparison and return either True
or False
:
>
,<
,>=
,<=
,==
,!=
print(10 > 5) # True
print(5 > 10) # False
print(10 < 5) # False
print(5 < 10) # True
print(10 <= 10) # True
print(10 >= 5) # True
print(10 == 10) # True
print(10 != 5) # True
a = 5
assigns the value5
toa
.a == 5
checks if the value ofa
is5
.
Shorthand operators for updating variables:
+=
(Add and assign)-=
(Subtract and assign)*=
(Multiply and assign)/=
(Divide and assign)%=
(Modulus and assign)//=
(Floor divide and assign)
a = 10
a += 2 # Now a is 12
a -= 5 # Now a is 7
a *= 2 # Now a is 14
a //= 5 # Now a is 2
a %= 2 # Now a is 0
print(a) # Output: 0
The increment (++
) and decrement (--
) operators are not available in Python.