Units 2/3 Vocab
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
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.
10
-10
12345678901234567890
-9876543210987654321
# All these numbers a valid integers in Python
3.14
-10.6
0.0
12345.6789
x = True
if x:
print("x is True")
else:
print("x is False")
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.
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()
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"
x = 10
y = 15
superheroes = [ "Superman", "Batman", "Wonder Woman", "The Flash", "Green Lantern", "Aquaman", "Martian Manhunter", "Cyborg", "Black Canary", "Captain Marvel"]
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
contacts = {
"Alice": "555-1234",
"Bob": "555-5678",
"Eve": "555-9101"
}
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.
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 = 5
while x > 0:
print(x)
x -= 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
string1 = 'Hello'
print(string1[0]) # Output: 'H'
print(string1[2]) # Output: 'l'
lst = [1, 2, 3, 4, 5]
len(lst)
s1 = 'Hello'
s2 = 'world'
s3 = s1 + ' ' + s2
print(s3)
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)
def func1():
print("I am learning Python Function")
def f(data):
return data * 5
def addition(first_number, second_number):
answer = first_number + second_number
return answer