Course: CSCI 1250

Assignment: Assignment 3 - Variables, Data Types, and Coding Standards

Variables

  • Containers for storing data
  • The value can change when a variable is mutable
  • Because Python is dynamically typed, variables must be declared and assigned in the same statement
    • You can use None if you don’t want a value yet.
  • In Python, you need a name and a value for a variable.
    • You can optionally add a type, such as str
cheese: str = "mozarella"
#       ^ optional type

Assignment

  • Assignment operator (=)
    • The value on the right is assigned to the variable on the left.
    • IE: cheese = "mozarella"
  • Variables can be re-assigned to any type.
    • If you have a type hint for the variable, your IDE should stop you, but Python itself doesn’t care.

Data Types

  • Numeric data and non-numeric data
    • int β†’ integers
    • float β†’ decimal numbers
    • str β†’ a list of characters; text data
    • bool β†’ True or False
age = 69 # int
gpa = 0.1 # float
name = "skibidi toilet" # str
is_cool = True # bool

Type Checking

  • You can check the type of a variable with the type() function in Python.
  • You can use this in a conditional expression, such as:
x = 3.0
 
print(type(x)) # <class 'float'>
print(type(x) === float) # True

Type Conversion (Casting)

  • Changes the data type of a variable to another
  • Normally you’d have to use a casting operator, such as as
  • In Python, you have to use the casting functions, such as int(), float(), and str()
    • These functions can raise errors when converting invalid values
    • float β†’ int will truncate
x = "some string"
x = int(x) # ValueError: invalid ltieral for int()
 
y = 2.99
y = int(y) # ⌊2.99βŒ‹ = 2

Math

Operators

  • + β†’ addition
  • - β†’ subtraction
  • * β†’ multiplication
  • / β†’ division
  • // β†’ floor division
  • % β†’ modulus
  • ** β†’ exponentiation

Division

  • In Python, division between two integers always results in a float, unless you use integer/floor division //:
x = 15
y = 7
 
x / y # 2.14... (float)
x // y # 2 (int)

Modulo

  • Returns the remainder of a division operation:
4 % 2 # 0
4 % 3 # 1

Strings

  • Represented by the str class in Python
  • Concatenation β†’ combining two or more strings
    • You can only concatenate strings with other strings (or you raise a TypeError)
    • IE: "me" + "ow" β†’ "meow"
  • Interpolation β†’ inserting different things into a string
    • You can use the .format method on strings, or an f-string
    • IE: f"i am {x} years old (where x is an int)

Methods

  • You can get the character from a certain index using the str[idx] syntax.
    • IE: "Hello"[0] β†’ "H", "Hello"[-1] β†’ "o"
  • You can get the length of a string using the len() function
    • IE: len("ABC") β†’ 3
  • You can slice strings using the str[start:stop:step] syntax.
    • The stop option is exclusive, and you can skip options
    • IE: "Hello"[0:4] β†’ "Hell", "Hello"[0::4] β†’ "Ho"

You can reverse a string with slicing by using [::-1]

Coding Standards

  • The style guide for Python is known as PEP 8
    • Use # for inline (single-line) comments
    • Use """ for block (multi-line) comments
    • Use snake_case for naming variables/functions; PascalCase for naming classes
    • Empty lines between functions/classes; two empty lines after import statements
    • Keep lines characters

Comments should describe the why not the what