Datatypes and Operators

Part 1: Essential Concepts for New Python Users

Overview of datatypes and operators in Python.

The First Cell

NoneRun, Edit, Run Again

Use the Run Code button to execute code live. Make edits and compare the output to figure out how Python works. If anything goes wrong, just reload the page to reset all code. And remember cell edits are not saved. Check out the notebook options, where you can download and save edits if you want to keep a record of your work.

Time to get started! The cell below will output the text when run. Be sure to pay attention to the syntax that determines how the cell is evaluated.

Update the string with a custom response like β€˜Hello, World!’ or β€˜Let’s dive in!’. Be sure to Run the cell after each update.

The above cell uses the Python print function (more on functions in the next lesson) to output a string that is passed to it. A string is a datatype in Python that represents text. Basic Python datatypes are presented next.

Datatypes

Python uses datatypes to organize and process information. The datatype can determine how information is stored, used, and displayed. Picking the right datatype makes coding much smoother. Key categories include numeric, strings, and sequences.

Key concepts of datatypes include:

-Item or Sequence/Collection: Datatypes like the numeric int and float represent an item while sequences and colletions can include multiple elements

-Ordered: Datatypes like lists and tuples keep items in order whereas dictionaries are unordered

-Mutable: Mutable datatypes including lists can changed after being defined whereas immutable datatypes like tuples can not

-Mappings: Dicionaries store data as key-value pairs

See Iteration and Flow Control for more on working with sequences and Boolean types often used for flow control.

Category Type Definition Example
Numbers int Whole number with no decimal or fraction 42
Numbers float Real number with decimal or fraction 3.14
Text str A sequence of characters β€œhello”
Sequences list Ordered, mutable collection of items [1, 2, 3]
Sequences tuple Ordered, immutable collection of items (1, 2, 3)
Mappings dict Unordered, mutable collection of key-value pairs {β€œa”: 1, β€œb”: 2}
Boolean bool True or False value True, False
Special NoneType Represents undefined or null value None

Datatypes in Code

The type function is used to verify types. Experiment by copying any of the examples into the below to replace the list. Note: Quotes are used to indicated text, so "hello" is valid but hello is not.

Python determines type based on syntax if not explicitly stated. This cell shows how set a number can be set as an integer or a string.

Operators

Operations in programming are used to manipulate and evaluate values. These allow programs to perform calculations, make decisions, and control the flow of execution.

Types include:

  • Mathematical
  • Logical
  • Comparison
  • Assignment

Mathematical

Mathematical operators perform arithmetic calculations. In Python, these include + (addition), - (subtraction), * (multiplication), and / (division). Python uses // for integer division. Regular division will return a float value, while integer division will discard the remainder and return an integer.

Not shown here, but available are also ** or ^ (exponentiation), and % or %% (modulus).

Operator Example Meaning Result
+ 3 + 2 Addition 5
- 7 - 4 Subtraction 3
* 6 * 2 Multiplication 12
/ 7 / 2 Division (always float) 3.5
// 7 // 2 Floor (integer) division 3

Use the cell below to run a addition example. Experiment with other operators and values.

Comparison

Comparison operators evaluate relationships between values. Common operators in Python include == (equal), != (not equal), < (less than), <= (less than or equal to), > (greater than), and >= (greater than or equal to).

These return boolean values of True/False.

Operator Example Meaning Result
== 3 == 3 Equal to True
!= 3 != 4 Not equal to True
> 5 > 2 Greater than True
< 2 < 5 Less than True
>= 5 >= 5 Greater than or equal to True
<= 4 <= 5 Less than or equal to True

Logical

Logical operators allow for boolean logic. In Python: and, or, not. These are used in conditionals and loops to combine multiple logical expressions.

Operator Example Meaning Result
and (3 > 1) and (2 < 5) True if both are true True
or (3 > 1) or (2 > 5) True if at least one is true True
not not (3 > 1) Negates (True β†’ False) False

Assignment

NoneAssignment and Equality

Assignment uses = to assign a value to a variable. x = 10 creates or updates the variable x to be 10.

Equality checking uses == and returns a Boolean (True, or False) depending on if the values are equal. x == 10 will return True if x is set as above.

Next Steps

Keep going with variables and functions β†’.