Programación I
Luis Llana Díaz
Departamento de Sistemas Informáticos y Computación
Universidad Complutense de Madrid
February 11, 2026
Computer Memory
Computer have two main types of memory:
Primary Memory (Main Memory, RAM)
(*) Large and small are very relative terms.
Computer Memory
Computer have two main types of memory:
Primary Memory (Main Memory, RAM)
Fast
(*) Large and small are very relative terms.
Computer Memory
Computer have two main types of memory:
Primary Memory (Main Memory, RAM)
Fast
Volatile
(*) Large and small are very relative terms.
Computer Memory
Computer have two main types of memory:
Primary Memory (Main Memory, RAM)
Fast
Volatile
Small capacity (*)
(*) Large and small are very relative terms.
Computer Memory
Computer have two main types of memory:
Primary Memory (Main Memory, RAM)
Fast
Volatile
Small capacity (*)
Permanent Memory (Storage)
(*) Large and small are very relative terms.
Computer Memory
Computer have two main types of memory:
Primary Memory (Main Memory, RAM)
Fast
Volatile
Small capacity (*)
Permanent Memory (Storage)
Slow
(*) Large and small are very relative terms.
Computer Memory
Computer have two main types of memory:
Primary Memory (Main Memory, RAM)
Fast
Volatile
Small capacity (*)
Permanent Memory (Storage)
Slow
Non-volatile
(*) Large and small are very relative terms.
Computer Memory
Computer have two main types of memory:
Primary Memory (Main Memory, RAM)
Fast
Volatile
Small capacity (*)
Permanent Memory (Storage)
Slow
Non-volatile
Large capacity (*)
(*) Large and small are very relative terms.
Primary Memory vs Permanent Memory
Primary memory is used to store data and programs that are
being used at the moment.
Primary Memory vs Permanent Memory
Primary memory is used to store data and programs that are
being used at the moment.
Permanent memory is used to store data and programs that
are not being used at the moment, but will be used in the
future.
Primary Memory vs Permanent Memory
Primary memory is used to store data and programs that are
being used at the moment.
Permanent memory is used to store data and programs that
are not being used at the moment, but will be used in the
future.
Data and programs are loaded from permanent memory to
primary memory when they are needed.
Primary Memory vs Permanent Memory
Primary memory is used to store data and programs that are
being used at the moment.
Permanent memory is used to store data and programs that
are not being used at the moment, but will be used in the
future.
Data and programs are loaded from permanent memory to
primary memory when they are needed.
Data and programs are saved from primary memory to
permanent memory when they are no longer needed.
How data is organized in RAM
Primary memory is
organized in cells.
Address Value
0x0000 25
0x0001 0
0x0002 65
0x0003 0
..
.
How data is organized in RAM
Primary memory is
organized in cells.
Address Value
Each cell has a unique
0x0000 25
address.
0x0001 0
0x0002 65
0x0003 0
..
.
How data is organized in RAM
Primary memory is
organized in cells.
Address Value
Each cell has a unique
0x0000 25
address.
0x0001 0
Each cell can store a fixed 0x0002 65
number of bits (usually 8 0x0003 0
bits = 1 byte). ..
.
How data is organized in RAM
Primary memory is
organized in cells.
Address Value
Each cell has a unique
0x0000 25
address.
0x0001 0
Each cell can store a fixed 0x0002 65
number of bits (usually 8 0x0003 0
bits = 1 byte). ..
.
Each cell can store a value
(data).
What is a variable?
Values are stored in cells of
memory. is_loaded
True
age 42
3.14159
pi_value Hello!
greeting
Computer Memory
What is a variable?
Values are stored in cells of
memory. is_loaded
True
age 42
3.14159
pi_value Hello!
greeting
Computer Memory
What is a variable?
Values are stored in cells of
memory. is_loaded
If my computer has 16Gb. of
RAM, it has approximately True
16 · 230 ≈ [Link]
cells. age 42
3.14159
pi_value Hello!
greeting
Computer Memory
What is a variable?
Values are stored in cells of
memory. is_loaded
If my computer has 16Gb. of
RAM, it has approximately True
16 · 230 ≈ [Link]
cells. age 42
A human cannot remember 3.14159
the address of each cell. pi_value Hello!
greeting
Computer Memory
What is a variable?
Values are stored in cells of
memory. is_loaded
If my computer has 16Gb. of
RAM, it has approximately True
16 · 230 ≈ [Link]
cells. age 42
A human cannot remember 3.14159
the address of each cell. pi_value Hello!
Instead of using the address
of the cell, we use a variable greeting
name to refer to the cell. Computer Memory
What is a variable?
Values are stored in cells of
memory. is_loaded
If my computer has 16Gb. of
RAM, it has approximately True
16 · 230 ≈ [Link]
cells. age 42
A human cannot remember 3.14159
the address of each cell. pi_value Hello!
Instead of using the address
of the cell, we use a variable greeting
name to refer to the cell. Computer Memory
A variable is a name that
refers to a cell in memory.
What is a variable?
Values are stored in cells of
memory. is_loaded
If my computer has 16Gb. of
RAM, it has approximately True
16 · 230 ≈ [Link]
cells. age 42
A human cannot remember 3.14159
the address of each cell. pi_value Hello!
Instead of using the address
of the cell, we use a variable greeting
name to refer to the cell. Computer Memory
A variable is a name that
refers to a cell in memory.
What is a variable?
Values are stored in cells of
memory. is_loaded
If my computer has 16Gb. of
RAM, it has approximately True
16 · 230 ≈ [Link]
cells. age 42
A human cannot remember 3.14159
the address of each cell. pi_value Hello!
Instead of using the address
of the cell, we use a variable greeting
name to refer to the cell. Computer Memory
A variable is a name that
refers to a cell in memory.
What is a variable?
Values are stored in cells of
memory. is_loaded
If my computer has 16Gb. of
RAM, it has approximately True
16 · 230 ≈ [Link]
cells. age 42
A human cannot remember 3.14159
the address of each cell. pi_value Hello!
Instead of using the address
of the cell, we use a variable greeting
name to refer to the cell. Computer Memory
A variable is a name that
refers to a cell in memory.
What is a variable?
Values are stored in cells of
memory. is_loaded
If my computer has 16Gb. of
RAM, it has approximately True
16 · 230 ≈ [Link]
cells. age 42
A human cannot remember 3.14159
the address of each cell. pi_value Hello!
Instead of using the address
of the cell, we use a variable greeting
name to refer to the cell. Computer Memory
A variable is a name that
refers to a cell in memory.
Assignation instruction
age = 42
greeting = 'Hello!'
is_loaded = True
pi_value = 3.14159
Assignation instruction
age = 42
greeting = 'Hello!'
is_loaded = True
pi_value = 3.14159
An assignation is an instruction with two parts (divided the by =
symbol)
Assignation instruction
age = 42
greeting = 'Hello!'
is_loaded = True
pi_value = 3.14159
An assignation is an instruction with two parts (divided the by =
symbol)
Right part. It is an expression.
Assignation instruction
age = 42
greeting = 'Hello!'
is_loaded = True
pi_value = 3.14159
An assignation is an instruction with two parts (divided the by =
symbol)
Right part. It is an expression.
Left part It is a variable.
Assignation instruction
age = 42
greeting = 'Hello!'
is_loaded = True
pi_value = 3.14159
An assignation is an instruction with two parts (divided the by =
symbol)
Right part. It is an expression.
Left part It is a variable.
The execution of the assignation instruction
Assignation instruction
age = 42
greeting = 'Hello!'
is_loaded = True
pi_value = 3.14159
An assignation is an instruction with two parts (divided the by =
symbol)
Right part. It is an expression.
Left part It is a variable.
The execution of the assignation instruction
1 The computer evaluates the expression.
Assignation instruction
age = 42
greeting = 'Hello!'
is_loaded = True
pi_value = 3.14159
An assignation is an instruction with two parts (divided the by =
symbol)
Right part. It is an expression.
Left part It is a variable.
The execution of the assignation instruction
1 The computer evaluates the expression.
2 It stores the result in the memory.
Assignation instruction
age = 42
greeting = 'Hello!'
is_loaded = True
pi_value = 3.14159
An assignation is an instruction with two parts (divided the by =
symbol)
Right part. It is an expression.
Left part It is a variable.
The execution of the assignation instruction
1 The computer evaluates the expression.
2 It stores the result in the memory.
3 The variable references to the cell of memory.
Execution of assignment instruction
is_loaded
True
age = 42 age 42
greeting = 'Hello!'
is_loaded = True 3.14159
pi_value = 3.14159 Hello!
pi_value
greeting
Computer Memory
Execution of assignment instruction
is_loaded
True
age = 42 age 42
greeting = 'Hello!'
is_loaded = True 3.14159
pi_value = 3.14159 Hello!
pi_value
greeting
Computer Memory
Execution of assignment instruction
is_loaded
True
age = 42 age 42
greeting = 'Hello!'
is_loaded = True 3.14159
pi_value = 3.14159 Hello!
pi_value
greeting
Computer Memory
Execution of assignment instruction
is_loaded
True
age = 42 age 42
greeting = 'Hello!'
is_loaded = True 3.14159
pi_value = 3.14159 Hello!
pi_value
greeting
Computer Memory
Execution of assignment instruction
is_loaded
True
age = 42 age 42
greeting = 'Hello!'
is_loaded = True 3.14159
pi_value = 3.14159 Hello!
pi_value
greeting
Computer Memory
Execution of assignment instruction
is_loaded
True
age = 42 age 42
greeting = 'Hello!'
is_loaded = True 3.14159
pi_value = 3.14159 Hello!
pi_value
greeting
Computer Memory
Execution of assignment instruction
is_loaded
True
age = 42 age 42
greeting = 'Hello!'
is_loaded = True 3.14159
pi_value = 3.14159 Hello!
pi_value
greeting
Computer Memory
Execution of assignment instruction
is_loaded
True
age = 42 age 42
greeting = 'Hello!'
is_loaded = True 3.14159
pi_value = 3.14159 Hello!
pi_value
greeting
Computer Memory
Execution of assignment instruction
is_loaded
True
age = 42 age 42
greeting = 'Hello!'
is_loaded = True 3.14159
pi_value = 3.14159 Hello!
pi_value
greeting
Computer Memory
The order is important
10
a
a = 10
a = a + 1 a 11
+ 1
Computer Memory
The order is important
10
a
a = 10
a = a + 1 a 11
+ 1
Computer Memory
The order is important
10
a
a = 10
a = a + 1 a 11
+ 1
Computer Memory
The order is important
10
a
a = 10
a = a + 1 a 11
+ 1
Computer Memory
The order is important
10
a
a = 10
a = a + 1 a 11
+ 1
Computer Memory
The order is important
10
a
a = 10
a = a + 1 a 11
+ 1
Computer Memory
Expressions
An expression is a combination of values, variables, and operators
that can be:
Expressions
An expression is a combination of values, variables, and operators
that can be:
Evaluated to produce a value.
Expressions
An expression is a combination of values, variables, and operators
that can be:
Evaluated to produce a value.
Used as part of an instruction.
Expressions
An expression is a combination of values, variables, and operators
that can be:
Evaluated to produce a value.
Used as part of an instruction.
The values and operators depends on the data types. The basic
data types in Python are
Expressions
An expression is a combination of values, variables, and operators
that can be:
Evaluated to produce a value.
Used as part of an instruction.
The values and operators depends on the data types. The basic
data types in Python are
Integer (int): whole numbers, e.g., 42, -3, 0.
Expressions
An expression is a combination of values, variables, and operators
that can be:
Evaluated to produce a value.
Used as part of an instruction.
The values and operators depends on the data types. The basic
data types in Python are
Integer (int): whole numbers, e.g., 42, -3, 0.
Floating-point (float): numbers with decimal points, e.g.,
3.14, -0.001. real numbers
Expressions
An expression is a combination of values, variables, and operators
that can be:
Evaluated to produce a value.
Used as part of an instruction.
The values and operators depends on the data types. The basic
data types in Python are
Integer (int): whole numbers, e.g., 42, -3, 0.
Floating-point (float): numbers with decimal points, e.g.,
3.14, -0.001. real numbers
String (str): sequences of characters, e.g., ”Hello”, ’Python’.
Expressions
An expression is a combination of values, variables, and operators
that can be:
Evaluated to produce a value.
Used as part of an instruction.
The values and operators depends on the data types. The basic
data types in Python are
Integer (int): whole numbers, e.g., 42, -3, 0.
Floating-point (float): numbers with decimal points, e.g.,
3.14, -0.001. real numbers
String (str): sequences of characters, e.g., ”Hello”, ’Python’.
Boolean (bool): logical values, either True or False.
What is number
1453
What is number
1453
MCMXLIII
What is number
1453
MCMXLIII
10110100101 (binary)
What is number
1453
MCMXLIII
10110100101 (binary)
������ ���� (old Babylonian)
Integers
Although our programs use ordinary base 10 representation of
numbers, the computers always work in binary (base 2
representation of numbers).
Byte
[<+->]
The basic data type in a computer is a byte: a sequence of 8
bits.
64-bit integers
Integers
Although our programs use ordinary base 10 representation of
numbers, the computers always work in binary (base 2
representation of numbers).
Byte
[<+->]
The basic data type in a computer is a byte: a sequence of 8
bits.
A bit can be either 0 or 1.
64-bit integers
Integers
Although our programs use ordinary base 10 representation of
numbers, the computers always work in binary (base 2
representation of numbers).
Byte
[<+->]
The basic data type in a computer is a byte: a sequence of 8
bits.
A bit can be either 0 or 1.
Therefore, a byte can represent 28 = 256 different values
(from 0 to 255).
64-bit integers
Integers
Although our programs use ordinary base 10 representation of
numbers, the computers always work in binary (base 2
representation of numbers).
Byte
[<+->]
The basic data type in a computer is a byte: a sequence of 8
bits.
A bit can be either 0 or 1.
Therefore, a byte can represent 28 = 256 different values
(from 0 to 255).
We need a byte to represent the sing (positive or negative).
So a byte can represent integers from -128 to 127.
64-bit integers
Integer operations
The basic operations on integers are
Addition: +, e.g., 2 + 3 = 5.
Integer operations
The basic operations on integers are
Addition: +, e.g., 2 + 3 = 5.
Subtraction: -, e.g., 2 - 5 = -3.
Integer operations
The basic operations on integers are
Addition: +, e.g., 2 + 3 = 5.
Subtraction: -, e.g., 2 - 5 = -3.
Multiplication: *, e.g., 4 * 3 = 12.
Integer operations
The basic operations on integers are
Addition: +, e.g., 2 + 3 = 5.
Subtraction: -, e.g., 2 - 5 = -3.
Multiplication: *, e.g., 4 * 3 = 12.
Exponentiation: **, e.g., 2 ** 3 = 8 (2 raised to the power
of 3).
Integer operations. Division
Let us remind the integer division
dividend = divisor · quotient + remainder
Integer operations. Division
Let us remind the integer division
dividend = divisor · quotient + remainder
Integer division: //, e.g., 10 // 3 = 3 (the quotient of the
integer division). the integer part of the division).
Integer operations. Division
Let us remind the integer division
dividend = divisor · quotient + remainder
Integer division: //, e.g., 10 // 3 = 3 (the quotient of the
integer division). the integer part of the division).
Modulus: %, e.g., 10 % 3 = 1 (is the remainder of the
division).
Integer operations. Division
Let us remind the integer division
dividend = divisor · quotient + remainder
Integer division: //, e.g., 10 // 3 = 3 (the quotient of the
integer division). the integer part of the division).
Modulus: %, e.g., 10 % 3 = 1 (is the remainder of the
division).
Division: /, e.g., 10 / 2 = 5.0 (note that the result is
always a float number, not an integer).
Examples
Expression result
2 + 3 * 4 14
(2 + 3) * 4 20
10 / 3 3.3333333333333335
10 // 3 3
10 % 3 1
4 * 2 ** 3 32
(4 * 2) ** 3 512
Precedences
The precedence of operators is as follows (from highest to lowest):
Parentheses: ‘()‘
Exponentiation: ‘**‘
Multiplication, Division, Integer Division, Modulus: ‘*‘, ‘/‘,
‘//‘, ‘
Addition and Subtraction: ‘+‘, ‘-‘
Examples
Expression result
2 + 3 * 4 14
(2 + 3) * 4 20
10 / 3 3.3333333333333335
10 // 3 3
10 % 3 1
4 * 2 ** 3 32
(4 * 2) ** 3 512
Precedences
The precedence of operators is as follows (from highest to lowest):
Parentheses: ‘()‘
Exponentiation: ‘**‘
Multiplication, Division, Integer Division, Modulus: ‘*‘, ‘/‘,
‘//‘, ‘
Addition and Subtraction: ‘+‘, ‘-‘
Examples
Expression result
2 + 3 * 4 14
(2 + 3) * 4 20
10 / 3 3.3333333333333335
10 // 3 3
10 % 3 1
4 * 2 ** 3 32
(4 * 2) ** 3 512
Precedences
The precedence of operators is as follows (from highest to lowest):
Parentheses: ‘()‘
Exponentiation: ‘**‘
Multiplication, Division, Integer Division, Modulus: ‘*‘, ‘/‘,
‘//‘, ‘
Addition and Subtraction: ‘+‘, ‘-‘
Floating point numbers
What is a real number?
Floating point numbers
What is a real number?
What is the value of π?
Floating point numbers
What is a real number?
What is the value of π?
Real numbers cannot be represented exactly in a computer
A computer can only represent a approximation of real numbers
Therefore, it uses an approximation of real numbers called
floating-point numbers.
Scientific Notation
0.9865687 · 1013
The mantissa is an integer (in this case 9865687). The
mantissa expresses the precision of the number.
Scientific Notation
0.9865687 · 1013
The mantissa is an integer (in this case 9865687). The
mantissa expresses the precision of the number.
The exponent is an integer (in this case 13). The exponent
expresses the scale of the number.
float64
Python uses float64 to represent floating-point numbers.
float64
Python uses float64 to represent floating-point numbers.
64 bits (8 bytes) to represent a floating-point number.
float64
Python uses float64 to represent floating-point numbers.
64 bits (8 bytes) to represent a floating-point number.
1 bit for the sign (positive or negative).
float64
Python uses float64 to represent floating-point numbers.
64 bits (8 bytes) to represent a floating-point number.
1 bit for the sign (positive or negative).
11 bits for the exponent (from -1023 to 1024). (10−308 to
10308 )
float64
Python uses float64 to represent floating-point numbers.
64 bits (8 bytes) to represent a floating-point number.
1 bit for the sign (positive or negative).
11 bits for the exponent (from -1023 to 1024). (10−308 to
10308 )
52 bits for the mantissa (from 0 to 252 − 1). (15-17 decimal
digits).
Operations
The basic operations on floating-point numbers are
Addition: +, e.g., 4.5 + 3.1 = 7.6.
Operations
The basic operations on floating-point numbers are
Addition: +, e.g., 4.5 + 3.1 = 7.6.
Subtraction: -, e.g., 5.0 - 2.2 = 2.8.
Operations
The basic operations on floating-point numbers are
Addition: +, e.g., 4.5 + 3.1 = 7.6.
Subtraction: -, e.g., 5.0 - 2.2 = 2.8.
Multiplication: *, e.g., 3.0 * 2.5 = 7.5.
Operations
The basic operations on floating-point numbers are
Addition: +, e.g., 4.5 + 3.1 = 7.6.
Subtraction: -, e.g., 5.0 - 2.2 = 2.8.
Multiplication: *, e.g., 3.0 * 2.5 = 7.5.
Division: /, e.g., 7.5 / 2.5 = 3.0. Always a floating-point
number, even if the result is an integer.
Problems
Let us work in base 10, with 4 digits of mantissa and 2 digits of
exponent.
0.1 · 101 ∗ 0.1 · 1010 = 0.01 · 1011 = 0.1 · 1010
Problems
Let us work in base 10, with 4 digits of mantissa and 2 digits of
exponent.
0.1 · 101 ∗ 0.1 · 1010 = 0.01 · 1011 = 0.1 · 1010
1.0 + 0.1 · 1010 . This is not easy,
Problems
Let us work in base 10, with 4 digits of mantissa and 2 digits of
exponent.
0.1 · 101 ∗ 0.1 · 1010 = 0.01 · 1011 = 0.1 · 1010
1.0 + 0.1 · 1010 . This is not easy,
Problems
Let us work in base 10, with 4 digits of mantissa and 2 digits of
exponent.
0.1 · 101 ∗ 0.1 · 1010 = 0.01 · 1011 = 0.1 · 1010
1.0 + 0.1 · 1010 . This is not easy, we have to convert
1.0 · 100 = 0. 0000000001
| {z } ·10
10
10digits
Problems
Let us work in base 10, with 4 digits of mantissa and 2 digits of
exponent.
0.1 · 101 ∗ 0.1 · 1010 = 0.01 · 1011 = 0.1 · 1010
1.0 + 0.1 · 1010 . This is not easy, we have to convert
1.0 · 100 = 0. 0000000001
| {z } ·10
10
10digits
Now we can add 0.1+ = 0.0000000001 = 0.1000000001
Problems
Let us work in base 10, with 4 digits of mantissa and 2 digits of
exponent.
0.1 · 101 ∗ 0.1 · 1010 = 0.01 · 1011 = 0.1 · 1010
1.0 + 0.1 · 1010 . This is not easy, we have to convert
1.0 · 100 = 0. 0000000001
| {z } ·10
10
10digits
Now we can add 0.1+ = 0.0000000001 = 0.1000000001
So 1 + 0.1 · 1010 = 0.1000000001 · 1010
Problems
Let us work in base 10, with 4 digits of mantissa and 2 digits of
exponent.
0.1 · 101 ∗ 0.1 · 1010 = 0.01 · 1011 = 0.1 · 1010
1.0 + 0.1 · 1010 . This is not easy, we have to convert
1.0 · 100 = 0. 0000000001
| {z } ·10
10
10digits
Now we can add 0.1+ = 0.0000000001 = 0.1000000001
So 1 + 0.1 · 1010 = 0.1000000001 · 1010
But we only have 4 digits of mantissa
0.1000000001 · 1010 = 0.1000 · 1010 = 0.1 · 1010
Problems
Let us work in base 10, with 4 digits of mantissa and 2 digits of
exponent.
0.1 · 101 ∗ 0.1 · 1010 = 0.01 · 1011 = 0.1 · 1010
1.0 + 0.1 · 1010 . This is not easy, we have to convert
1.0 · 100 = 0. 0000000001
| {z } ·10
10
10digits
Now we can add 0.1+ = 0.0000000001 = 0.1000000001
So 1 + 0.1 · 1010 = 0.1000000001 · 1010
But we only have 4 digits of mantissa
0.1000000001 · 1010 = 0.1000 · 1010 = 0.1 · 1010
Therefore 1 + 0.1 · 1010 = 0.1 · 1010
Problems
Let us work in base 10, with 4 digits of mantissa and 2 digits of
exponent.
0.1 · 101 ∗ 0.1 · 1010 = 0.01 · 1011 = 0.1 · 1010
1.0 + 0.1 · 1010 . This is not easy, we have to convert
1.0 · 100 = 0. 0000000001
| {z } ·10
10
10digits
Now we can add 0.1+ = 0.0000000001 = 0.1000000001
So 1 + 0.1 · 1010 = 0.1000000001 · 1010
But we only have 4 digits of mantissa
0.1000000001 · 1010 = 0.1000 · 1010 = 0.1 · 1010
Therefore 1 + 0.1 · 1010 = 0.1 · 1010
We have lost the precision of the number 1, and we have a
wrong result.
Booleans
Type bool has only two values: True and False.
Booleans
Type bool has only two values: True and False.
Booleans are the result of comparisons between values, e.g.,
2 > 3 is False, 5 == 5 is True.
Booleans
Type bool has only two values: True and False.
Booleans are the result of comparisons between values, e.g.,
2 > 3 is False, 5 == 5 is True.
Booleans are used to control the flow of the program, e.g., in
if statements and while loops.
Bool operations
The basic operations on booleans are
Logical AND: and
A B A and B
True True True
True False False
False True False
False False False
Bool operations
The basic operations on booleans are
Logical AND: and
Logical OR: or
A B A and B A B A or B
True True True True True True
True False False True False True
False True False False True True
False False False False False False
Bool operations
The basic operations on booleans are
Logical AND: and
Logical OR: or
Logical NOT: not
A B A and B A B A or B
True True True True True True
True False False True False True
False True False False True True
False False False False False False
A not A
True False
False True
Comparison operators
The basic comparison operators are
Equal: ==
Comparison operators
The basic comparison operators are
Equal: ==
Not equal: !=
Comparison operators
The basic comparison operators are
Equal: ==
Not equal: !=
Greater than: >
Comparison operators
The basic comparison operators are
Equal: ==
Not equal: !=
Greater than: >
Less than: <
Comparison operators
The basic comparison operators are
Equal: ==
Not equal: !=
Greater than: >
Less than: <
Greater than or equal to: >=
Comparison operators
The basic comparison operators are
Equal: ==
Not equal: !=
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
Strings
A string is a sequence of characters. In Python, strings are
enclosed in either single quotes ('Hello') or double quotes
("Hello"). The basic operations on strings are
Strings
A string is a sequence of characters. In Python, strings are
enclosed in either single quotes ('Hello') or double quotes
("Hello"). The basic operations on strings are
The length of a string: len()
Strings
A string is a sequence of characters. In Python, strings are
enclosed in either single quotes ('Hello') or double quotes
("Hello"). The basic operations on strings are
The length of a string: len()
Indexing: [] Hello[1] is ’e’, Hello[4] is ’o’.
Strings
A string is a sequence of characters. In Python, strings are
enclosed in either single quotes ('Hello') or double quotes
("Hello"). The basic operations on strings are
The length of a string: len()
Indexing: [] Hello[1] is ’e’, Hello[4] is ’o’.
Concatenation: +
Examples
>>> s = 'Hello '
>>> len(s)
5
>>> s[0], s[1], s[2], s[3], s[4]
('H', 'e', 'l', 'l', 'o')
Examples
>>> s = 'Hello '
>>> len(s)
5
>>> s[0], s[1], s[2], s[3], s[4]
('H', 'e', 'l', 'l', 'o')
Important: In Python (and in most computer languages)
enumerations start at 0.
Examples
>>> s = 'Hello '
>>> len(s)
5
>>> s[0], s[1], s[2], s[3], s[4]
('H', 'e', 'l', 'l', 'o')
Important: In Python (and in most computer languages)
enumerations start at 0.
>>> name = 'Alice '
>>> greeting = 'Hello , ' + name + '!'
>>> greeting
'Hello , Alice!'
Characters encodings
Python uses UTF-8 encoding for strings.
Characters encodings
Python uses UTF-8 encoding for strings.
ASCII
First computers used ASCII enconding (1961)
Only U.S. chars
1 byte, 1 letter
8-bit encoding, 7 (bits) for information, 1 bit for parity check.
ASCII table
[Link]
011 0000 48 0
011 0001 49 1
011 0010 50 2
011 0011 51 3
011 0100 52 4
011 0101 52 5
011 0110 54 6
011 0111 55 7
011 1000 56 8
011 1001 57 9
ASCII table
[Link]
011 0000 48 0 100 0001 65 A
011 0001 49 1 100 0010 66 B
011 0010 50 2 100 0011 67 C
011 0011 51 3 100 0100 68 D
011 0100 52 4 100 0101 69 E
011 0101 52 5 …
011 0110 54 6 101 0111 87 V
011 0111 55 7 101 1000 88 X
011 1000 56 8 101 1001 89 Y
011 1001 57 9 101 1010 90 Z
ASCII table
[Link]
011 0000 48 0 100 0001 65 A 110 0001 97 a
011 0001 49 1 100 0010 66 B 110 0010 98 b
011 0010 50 2 100 0011 67 C 110 0011 99 c
011 0011 51 3 100 0100 68 D 110 0100 100 d
011 0100 52 4 100 0101 69 E 110 0101 101 e
011 0101 52 5 … …
011 0110 54 6 101 0111 87 V 110 0111 119 v
011 0111 55 7 101 1000 88 X 111 1000 120 x
011 1000 56 8 101 1001 89 Y 111 1001 121 y
011 1001 57 9 101 1010 90 Z 111 1010 122 z
Other encodings
Since other languages have more characters than English, other
encodings were developed, e.g.,
Other encodings
Since other languages have more characters than English, other
encodings were developed, e.g.,
ISO-8859-1 (Latin-1) , that can represent characters from Western
European languages. Up to 15 ISO-8859 charsets. 1
byte, 1 letter.
Other encodings
Since other languages have more characters than English, other
encodings were developed, e.g.,
ISO-8859-1 (Latin-1) , that can represent characters from Western
European languages. Up to 15 ISO-8859 charsets. 1
byte, 1 letter.
windows-1252 Microsoft developed. 1 byte, 1 letter
Other encodings
Since other languages have more characters than English, other
encodings were developed, e.g.,
ISO-8859-1 (Latin-1) , that can represent characters from Western
European languages. Up to 15 ISO-8859 charsets. 1
byte, 1 letter.
windows-1252 Microsoft developed. 1 byte, 1 letter
Problems:
Other encodings
Since other languages have more characters than English, other
encodings were developed, e.g.,
ISO-8859-1 (Latin-1) , that can represent characters from Western
European languages. Up to 15 ISO-8859 charsets. 1
byte, 1 letter.
windows-1252 Microsoft developed. 1 byte, 1 letter
Problems:
Many different incompatible charsets
Other encodings
Since other languages have more characters than English, other
encodings were developed, e.g.,
ISO-8859-1 (Latin-1) , that can represent characters from Western
European languages. Up to 15 ISO-8859 charsets. 1
byte, 1 letter.
windows-1252 Microsoft developed. 1 byte, 1 letter
Problems:
Many different incompatible charsets
Many languages have more than 256 letters (1 byte)
Unicode
Unicode is a character encoding standard maintained by the
Unicode Consortium designed to support the use of text in all of
the world’s writing systems that can be digitized.
[Link]
Unicode
Unicode is a character encoding standard maintained by the
Unicode Consortium designed to support the use of text in all of
the world’s writing systems that can be digitized.
[Link]
UTF-8. Most used (Python). Backwards compatible with
ASCII.
Unicode
Unicode is a character encoding standard maintained by the
Unicode Consortium designed to support the use of text in all of
the world’s writing systems that can be digitized.
[Link]
UTF-8. Most used (Python). Backwards compatible with
ASCII.
UTF-16 and UTF-32.
Unicode
Unicode is a character encoding standard maintained by the
Unicode Consortium designed to support the use of text in all of
the world’s writing systems that can be digitized.
[Link]
UTF-8. Most used (Python). Backwards compatible with
ASCII.
UTF-16 and UTF-32.
Problem: Many documents in Internet use old encodings.