Unit 2 Binary/Data Terms

Bits

A bit is a unit of information that can have only two values: 0 or 1. It is the smallest unit of information that can be stored or processed by a computer. Bits are typically used to represent binary data, which is data that is encoded using only two possible values. In Python you can use "int" data type to represent binary data as a sequence of bits.

binary_number = 0b1010
# represents 1010 as an integer

Bytes

A byte is a group of 8 bits. It is a unit of information that can hold a single character in a computer's memory.

Hexadecimals/Nibbles

  • a hexadecimal is a base-16 number represented using the symbols 0-9 and A-F. Hexadecimals are often used to represent colors in web development, as they can be more concise and easier to read than longer binary numbers.
  • A nibble is a unit of data that is half of a byte (eight bits). It is often represented using a hexadecimal digit, which can represent a value from 0 to 15 in decimal.

Unsigned Integer

An unsigned integer is a type of integer that can only represent non-negative (greater than or equal to zero) whole numbers. It is called "unsigned" because it does not have a sign bit to indicate whether the number is positive or negative. This means that an unsigned integer can represent a larger range of values than a signed integer, which uses a sign bit to indicate the sign of the number.

Signed Integer

An integer is a whole number that can be either positive or negative. It has no decimal point and can store very large numbers. By default, Python integers are stored as signed integers, which means they can represent both positive and negative numbers.

10
-10
12345678901234567890
-9876543210987654321
# All these numbers a valid integers in Python

Floating Point

A floating point number is a number with a decimal point. It is used to represent real numbers, such as 3.14159 or -0.01.

For example, the following are all valid floating point numbers in Python:

3.14
-10.6
0.0
12345.6789

Boolean

A boolean is a data type that represents a truth value. It can have two values: True or False. Booleans are often used in conditional statements to control the flow of a program

x = True
if x:
    print("x is True")
else:
    print("x is False")

ASCII

ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents text as a series of numbers. It defines a mapping between numerical values and characters, with each character being represented by a unique number between 0 and 127.

Unicode

Unicode is a character encoding standard that represents text as a series of numbers. It is a widely used standard that supports a much larger range of characters than ASCII, including non-Latin scripts and emojis.

In Python, Unicode characters are represented using the str data type, which is a sequence of Unicode code points.

RGB

A problem solving approach (algorithm) to find a satisfactory solution where finding an optimal or exact solution is impractical or impossible.

Lossy

In the context of digital media, "lossy" refers to a type of data compression that reduces the quality of the data in order to save space. Lossy compression algorithms remove data that is not perceived by the human eye or ear, resulting in a smaller file size but lower quality. In Python, you can use various libraries and modules to work with lossy data formats. For

from PIL import Image

# Open a JPEG image and save it as a PNG image
image = Image.open('image.jpg')
image.save('image.png')

Lossless

Lossless compression algorithms remove redundant or unnecessary data from the file, but do not remove any data that is needed to accurately represent the original data. Lossless compression is commonly used for text, data, and other types of files where it is important to preserve the accuracy of the data. For example, the ZIP file format and the PDF document format are both lossless formats that are widely used for storing and distributing digital documents.

import zipfile

# Create a ZIP file
with zipfile.ZipFile('documents.zip', 'w') as z:
    z.write('document1.txt')
    z.write('document2.txt')

# Extract the contents of the ZIP file
with zipfile.ZipFile('documents.zip', 'r') as z:
    z.extractall()

Unit 3 Algorithm/Programming Terms

Variables

A variable is a named location in memory that stores a value or reference to a value. When you create a variable, you can specify the value that you want to store in it. You can then use the variable to refer to the value throughout your program.

For example, you might create a variable called name and store your name in it:

name = "Derek"

Data Types

In Python, data types are used to define the type of a value that can be stored in a variable.

ex: "int", "float", "str", etc.

Assignment Operators

assignment operators are used to assign a value to a variable. The most basic assignment operator is the = operator, which assigns the value on the right side of the operator to the variable on the left side.

x = 10
y = 15

Lists

A list is a collection of items that are ordered and changeable. Lists are written with square brackets and the items are separated by commas.

superheroes = [    "Superman",    "Batman",    "Wonder Woman",    "The Flash",    "Green Lantern",    "Aquaman",    "Martian Manhunter",    "Cyborg",    "Black Canary",    "Captain Marvel"]

2D Lists

A 2D list is a list of lists. It is a data structure that allows you to store a collection of items, where each item is a list itself.

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

Dictionaries

A dictionary is a collection of key-value pairs. It is an unordered data structure that allows you to store and retrieve data using keys rather than indices.

contacts = {
    "Alice": "555-1234",
    "Bob": "555-5678",
    "Eve": "555-9101"
}

Class

a class is a blueprint for creating objects. It defines the properties and behaviors that the objects will have.

class Dog:
    def __init__(self, name, breed, age):
        self.name = name
        self.breed = breed
        self.age = age

Algorithms, Sequences, Selection, Iteration

Algorithms: a list set of instructions, used to solve problems or perform tasks.

Sequence: algorithms do tasks in the order of specification.

Selection: helps choose two different outcomes based off a decision.

Iteration: if a condition is true, then the code can repeat.

Expressions

An expression is a piece of code that returns a value. Expressions are made up of variables, operators, and function calls, and they can be used in a variety of contexts, such as assignments, function calls, and control structures.

Comparison Operators

Comparison operators are used to compare the values of two expressions. They return a Boolean value indicating whether the comparison is true or false.

ex: 'x == y': returns True if x is equal to y, and False otherwise.

Boolean Expressions and Selection

a Boolean expression is an expression that evaluates to either True or False. Boolean expressions are often used in control structures, such as if statements and while loops, to determine whether a block of code should be executed.

Here is an example of an if statement using a Boolean expression in Python:

x = 5
if x > 0:
    print("x is positive")
x is positive

Boolean Expressions and Iteration

You can use Boolean expressions in iteration structures, such as for loops and while loops, to control the flow of the loop.

Here is an example of a while loop using a Boolean expression in Python:

x = 5
while x > 0:
    print(x)
    x -= 1
5
4
3
2
1

Truth Tables

A truth table is a way to represent the relationship between different logical statements or expressions. It lists all of the possible combinations of input values for the statements or expressions, and shows the corresponding output values that result.

have two values

0 = off, false

1 = on, true

Examples

0 and 0 = false. And operator means both needs to be true.

0 or 1 = true. Either or.

Use this in conditionals (selection).

XOR = exclusive or.

Or = similar to true or false. -

Ex: A is true, B is false

Characters

A character is a single Unicode character, which is a unit of text that represents a letter, number, symbol, or other type of character. Unicode is a standardized character encoding system that represents characters from many different languages and scripts.

Strings

A string is a sequence of characters. Strings are used to represent text and are often used to store and manipulate data.

string1 = 'Hello'
print(string1[0])  # Output: 'H'
print(string1[2])  # Output: 'l'
H
l

Length

The len() function returns the length of an object. For example, if you have a string, a list, or a tuple, you can use len() to determine the number of elements it contains.

Here's an example of using len() with a string:

lst = [1, 2, 3, 4, 5]
len(lst)
5

Concatenation

Concatenation refers to the operation of joining two or more strings together to create a single string. This can be done using the + operator, which is used to add two or more strings together.

 s1 = 'Hello'
 s2 = 'world'
 s3 = s1 + ' ' + s2
 print(s3)
Hello world

Upper

Used to check if the argument contains any uppercase characters.

Lower

Returns the lowercase string from the given string.

Traversing Strings

The process of going through a String one character at a time, often using loops.

Python If, Elif, Else Conditionals

If: statement executes a piece of code when one statement is false and the following statement is true.

Elif: first if statement isn't true, but want to check for another condition.

Else: executes if "if" isn't true.

hours = 30
sal = ""
experienced = True
# using operators to determine the salary
if (hours >= 10):
    sal = "130k"
elif (hours >= 8):
    sal = "90k"
else:
    sal = "50k"
    experienced = False
# printing the final statement 
print ('Derek has a salary of:' , sal,'and has experience:' , experienced)
Derek has a salary of: 130k and has experience: True

Nested Selection Statements

Conditional within a conditional. When more than one decision must be made before the appropriate action can be taken.

Python For, While loops with Range, with List

Returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

Combining loops with conditionals to break, continue

A statement that controls the flow of execution depending on some condition.

Procedural Abstraction

A function encapsulates a bundle of program statements (code): The function gives us a name for the statements, a way to invoke them at a high-level.

Python Def procedures

def is the keyword for defining a function.

def func1():
    print("I am learning Python Function")

Parameters

A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is sent to the function when it is called.

def f(data):
    return data * 5

Return Values

The value that a function returns to the caller is generally known as the function's return value. All Python functions have a return value, either explicit or implicit.

def addition(first_number, second_number):
    answer = first_number + second_number
    return answer