Skip to main content

Computer Programming: Primitive Data Types

Primitive Data Types

  • Byte: 8 bits, 1 byte, raw data
  • Integer
    • Signed: Any negative integer
    • Unsigned: Any non-negative integer (including zero)
  • Short: A signed or unsigned integer with more memory allocation than an integer
  • Long: A signed or unsigned integer with yet more memory allocation than an integer or short
  • Floating Point / Double: An integer with a dotted decimal value
  • Boolean: true or false, off or on, 1 or 0
  • Char: A single, individual character
  • Strings: A sequence of characters wrapped in single or double quotation marks
    • A like to visualize a physical string and you're threading characters onto the string

Loosely and Strongly Typed Languages

Loosely Typed

  • Some programming languages such as JavaScript, PowerShell, and Python will make a best effort to infer the type of data being processed when not explicitly defined
    • PowerShell and Python both allow the programmer to explicitly specify a data type as needed. In these instances, the data type must match or an exception will be thrown.
      • This may be necessary in the event that the programmer wants to run specific operations on the input that would only be possible given a specific data type
        • Example: You want to find the byte value for every character in a string
    • Bash has no concept of data types

Strongly Typed

  • Other programming languages such as TypeScript, C++, C#, and rust -- to name a few 
    • This means that the programmer must explicitly state up front what kind of data they expect to deal with when given an input
    • And if the input does not conform to the declared data type, an exception will be thrown

Variables and User Input

  • Let's take a brief detour to variables, since you'll be seeing them in the code snippets below
    • Variables are used to define values or store user input
    • The values are then archived at a specific address in RAM
    • When the variable's name is referenced the value is retrieved from memory
  • You'll often hear variables compared to buckets
    • Put a value in the bucket
    • Take it back out when you need it
  • I prefer to just call variables for what they really are... placeholders
    • We'll use a restaurant menu as an example

1

  • Chicken Sandwich
  • Side Salad
  • Drink

2

  • Steak
  • Soup
  • Side Salad
  • Drink

3

  • Seafood Pasta
  • Side Salad
  • Drink
  • Customers can order from the menu by choosing the corresponding number

I'd like number 1

  • And, the restaurant staff will know exactly what the customer ordered
  • Even if the restaurant changes number 1 to something different:
    • Pulled pork
    • Mac and cheese
    • Drink
  • The customer can always say number 1 and the restaurant will know what the customer ordered

Variables in PowerShell

Variables in PowerShell
# Stylistically speaking, PowerShell variable names should use camel casing
# There's no reason you cannot use underscores between words
# That's just not adhering to convention

# Your variable names should be clear and descriptive
# Others reading your code should be able to infer the reason for the variable
$firstName = 'Ben'
$favoriteFood = Read-Host -Prompt "Hello, $firstName! Enter your favorite food"
$favoriteHobby = Read-Host -Prompt "Thank you, $firstName! Please also tell me your favorite pastime activity"

Write-Host "User: $firstName"
Write-Host "Favorite food: $favoriteFood"
Write-Host "Favorite pastime activity: $favoriteHobby"

$firstName = 'Dan'
$favoriteFood = Read-Host -Prompt "Hello, $firstName! Enter your favorite food"
$favoriteHobby = Read-Host -Prompt "Thank you, $firstName! Please also tell me your favorite pastime activity"

# Even if the variables change, as is the case above
# We can continue to reference the placeholder name throughout the lifetime of the script
# Once the script terminates and exits, the variables are removed from memory
Write-Host "User: $firstName"
Write-Host "Favorite food: $favoriteFood"
Write-Host "Favorite pastime activity: $favoriteHobby"

Variables in Python

Variables in Python
# Stylistically speaking, Python variables use underscores to separate words
# There's no reason you cannot use camel casing, that's just not adhering to convention

# Your variable names should be clear and descriptive
# Others reading your code should be able to infer the reason for the variable
first_name = 'Ben'
favorite_food = input(f"Hello, {first_name}! Enter your favorite food: ")
favorite_hobby = input(f"Thank you, {first_name}! Please also tell me your favorite pastime activity: ")

print(f"User: {first_name}")
print(f"Favorite food: {favorite_food}")
print(f"Favorite pastime activity: {favorite_hobby}")

first_name = 'Dan'
favorite_food = input(f"Hello, {first_name}! Enter your favorite food: ")
favorite_hobby = input(f"Thank you, {first_name}! Please also tell me your favorite pastime activity: ")

# Even if the variables change, as is the case above
# We can continue to reference the placeholder name throughout the lifetime of the script
# Once the script terminates and exits, the variables are removed from memory
print(f"User: {first_name}")
print(f"Favorite food: {favorite_food}")
print(f"Favorite pastime activity: {favorite_hobby}")

Data Types in Action

  • Data types are important as you'll see in the examples below
  • They allow the programmer to ensure the user has provided an input of a required type
  • This not only helps to ensure the functionality of the program, but also helps with the security of the program

PowerShell

Data Types in PowerShell
$integer = 10
$float = 10.1
$short = 123456
$long = 12345678987654321
$boolean = $false

# A string may be wrapped in
# Single quotes ' '
# Or, double quotes " "
$string1 = 'hello world!'
$string2 = "100"

# Must specify [char] here
# Because PowerShell will try to cast it as string
[char]$char1 = 'y'
[char]$char2 = '1'

$integer.GetType()
$float.GetType()
$short.GetType()
$long.GetType()
$boolean.GetType()
$string1.GetType()
$string2.GetType()
$char1.GetType()
$char2.GetType()

# Request an integer from the user
[int]$integerFromUser = Read-Host -Prompt "Please provide an integer"

# PowerShell will interpret this an integer
100

# PowerShell will interpret this as a string
'a' 

# PowerShell will interpret this as a double
3.14

# Cast the integer as a string
[String]100

# Cast the double as a string
[String]3.14

# Cast 'a' as a char, not a string
[Char]'a'

# Cast 'f' as a byte
[Byte]'f'

# True, on
[Bool]1

# False, off
[Bool]0

# String indexing and slicing
$string = 'abcdef'
# Indexing
$string[0] # prints a
$string[4] # prints e
# Slicing
$string[0..2] # prints abc

Python

Data Types in Python
integer = 10 # Python identifies this as <class 'int'>
floating_point = 10.1
short = 123456 # Python identifies this as <class 'int'>
long = 1234567898765432109876543210 # Python identifies this as <class 'int'>
boolean = False

# A string may be wrapped in
# Single quotes ' '
# Or, double quotes " "
string_1 = 'hello world!'
string_2 = "100"

# In PowerShell we can cast these type [char]
# Python will treat these as single character strings
char_1 = 'y'
char_2 = '1'

print(type(integer))
print(type(floating_point))
print(type(short))
print(type(long))
print(type(boolean))
print(type(string_1))
print(type(string_2))
print(type(char_1))
print(type(char_2))

# Request an integer from the user
integer_from_user = int(input(f"Please provide an integer: "))

# Python will interpret this as an integer
print(type(100))

# Python will interpret this as a string
print(type('a'))

# Python will interpret this as a float
print(type(3.14))

# Cast the integer as a string
print(type(str(100)))

# Cast the float as a string
print(type(str(3.14)))

# Cast 'f' as a byte
print(ord('f'))

# True, on
print(bool(1))

# False, off
print(bool(0))

# String indexing and slicing
string = 'abcdef'
# Indexing
print(string[0])  # prints a
print(string[4])  # prints e
# Slicing
print(string[0:3])  # prints abc

 

String Formatting and Interpolation

PowerShell

# String Interpolation Examples
# Define some strings
$name = 'Ben'
$drink = 'coffee'

# Variable Substitution
Write-Host "Hello! My name is $name and I like $drink."

# String Formatting
Write-Host ("Hello! My name is {0} and I like {1}." -f $name, $drink)

Python

# String Interpolation Examples
# Define some strings
name = 'Ben'
drink = 'coffee'

# String Formatting
print(f"Hello! My name is {name} and I like {drink}.")
print("Hello! My name is %s and I like %s." % (name, drink))
print("Hello! My name is {} and I like {}.".format(name, drink))

Operators

  • Operators help computer programmers when comparing data types
  • These operators are used predominantly in conditional logic, which we'll be covering in the next step

Operators in PowerShell

Arithmetic Operators

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulo (divide and return remainder)

Relational Operators

  • -eq equals
  • -ne not equals
  • -gt greater than
  • -lt less than
  • -ge greater than or equal to
  • -le less than or equal to
  • And many, many more...

More PowerShell Operators: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators

PowerShell Operators Examples
10 + 10 # returns 20
30 - 15 # returns 15
2 * 2 # returns 4
23 / 3 # returns 7.66666666666667
23 % 3 # returns 2

1 -eq 1 # returns True
2 -eq 1 # returns False
'string' -eq 'string' # returns True
'String' -eq 'string' # returns True
'String' -ceq 'string' # returns False

3 -ne 2 # returns True
3 -ne 3 # returns False
'string' -ne 'string' # returns False
'String' -ne 'string' # returns False
'String' -cne 'string' # returns True

5 -gt 4 # returns True
'a' -gt 'b' # returns False

6 -lt 3 # returns False
'a' -lt 'b' # returns True

4 -le 4 # returns True
5 -ge 5 # returns True

Operators in Python

Arithmetic Operators

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulo (divide and return remainder)

Relational Operators

  • == equals
  • != not equals
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to
  • And many, many more...

More Python Operators: https://www.programiz.com/python-programming/operators

Python Operators Examples
10 + 10 # returns 20
30 - 15 # returns 15
2 * 2 # returns 4
23 / 3 # returns 7.66666666666667
23 % 3 # returns 2

1 == 1 # returns True
2 == 1 # returns False
'string' == 'string' # returns True
'String' == 'string' # returns False

3 != 2 # returns True
3 != 3 # returns False
'string' != 'string' # returns False
'String' != 'string' # returns True

5 > 4 # returns True
'a' > 'b' # returns False

6 < 3 # returns False
'a' < 'b' # returns True

4 <= 4 # returns True
5 >= 5 # returns True