1 Intro & Basic Data Types

Quick Start

  • When you type something into the Python Console/REPL, it immediately evaluates and shows the result.
  • In notebooks (like Jupyter/Quarto), the last expression in a cell is also displayed.
2 + 4
6
  • As expected, Python returned 6.

Arithmetic Operations

  • Python can be used as a calculator.
  • Common operators: +, -, *, /, ** (power), // (floor division), % (modulus)
(59 + 73 + 2) / 3 
44.666666666666664
10**(3+1)               # exponentiation (** is power, not ^)
10000
3**2                     # 3 squared
9
10 // 3                  # floor division (integer quotient)
3
10 % 3                   # remainder (modulus)
1

Creating Variables

  • A variable is a name that references an object (value) in memory.
  • Assignment (=) binds the name (left) to the object (right).
x = 48
  • Multiple assignment (tuple unpacking) is concise and common:
a, b, c, d = 7, 5, 8, 4
print(a, b, c, d)
7 5 8 4
  • Swapping variables without a temporary:
a, b = b, a
print(a, b)
5 7

Naming Variables

  • Valid characters:
    • letters (A–Z, a–z)
    • digits (0–9)
    • underscore (_)
  • Rules:
    • Cannot start with a digit.
    • Names may start with _ (e.g., _cache), but that implies “internal” by convention.
    • Reserved keywords cannot be used (e.g., True, False, None, and, class, def, …).
Style (PEP 8)
  • Use snake_case for variables/functions: total_count, is_ready.
  • Use UPPER_CASE for constants: PI = 3.14159.

Basic Scalar Data Types

Every value has a type describing what it represents and how it behaves.

  • int — integers (0, -7, 42)
  • float — double-precision real numbers (3.14, -0.001, 1.0)
  • bool — logical values (True, False) — behave like ints (True==1, False==0)
  • str — immutable Unicode text ("Dhaka", "Data Science")
x = 10            # int
y = 3.5           # float
name = "Rasel"    # str
is_ready = True   # bool
print(type(x), type(y), type(name), type(is_ready))
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>

Strings

  • Strings can use single ' or double " quotes.
a = 'one way of writing a string'
b = "another way"
  • Multi-line strings use triple quotes:
c = """
This is a longer string that
spans multiple lines
"""
print(c)

This is a longer string that
spans multiple lines

Data Types and Operations

  • Types determine what operations are allowed:
    • numeric + numeric → OK
    • string + string → concatenation
    • numeric + string → TypeError
17 + 4.3
21.3
17 + "seventeen"      # raises TypeError (number + string)
"This" + "That"
'ThisThat'

Explicit Type Conversion (Type Casting)

  • The str, bool, int, and float types are also functions that can be used to cast values to those types:
s = '3.14159'
fval = float(s)       # string → float
type(fval)
float
ival = int(fval)      # float → int (truncates toward zero)
print(ival)
3
bval = bool(ival)     # non-zero → True; zero → False
print(bval)
True
Note

int("3.5") fails; you must convert to float first, then to int if needed.

The None Type

  • Python provides a special value called None that is used to indicate the absence of a value.
  • The purpose of None is similar to NULL in R
a = None
a is None, a == None
(True, True)
  • If a function does not explicitly return a value, it implicitly returns None.

Equality vs Identity

  • == checks value equality; is checks object identity (same memory address).
x = 3000
y = 3000
z = x
x == y, x is y, x is z   # equal values
(True, False, True)

Printing & f-Strings

  • Use print() to display values intentionally.
  • f-strings make formatted strings easy.
name = "Rasel"
score = 83.4567
print(f"Hello, {name}! Your score is {round(score, 0)}.")
Hello, Rasel! Your score is 83.0.

Comments & Help

  • Single-line comment: # this is a comment
  • Built-in help:
help(len)          # opens inline help
Help on built-in function len in module builtins:

len(obj, /)
    Return the number of items in a container.

Augmented Assignment

  • An augmented assignment is a programming shortcut that combines an operation and an assignment into a single statement, making code more concise and often more readable
n = 10
n += 5    # same as n = n + 5
n *= 2
n
30

Floating-point Rounding Issue

  • Floating-point numbers inherently experience rounding issues due to the way computers represent real numbers
a = 0.1 + 0.2
a == 0.3, a
(False, 0.30000000000000004)
  • Compare using math.isclose()
import math
math.isclose(a, 0.3)
True

Comparison and Logical Operators

  • Used to compare two operands and return a Boolean result (True or False).

  • Comparison operators (similar to R’s symbols):

Meaning Operator
equal to ==
not equal !=
greater / greater-equal >, >=
less / less-equal <, <=
10 <= 15
True
14 != 16
True

  • Chaining is allowed and reads naturally:
x = 3
0 < x < 5, (0 < x and x < 5)
(True, True)
  • Logical operators: and, or, not
(2 < 5) and (10 > 3), (2 > 5) or (10 > 3), not False
(True, True, True)

Summary

  • Use ** for power, not ^.
  • Understand == vs is.
  • Floats are approximate; use math.isclose for comparisons.
  • Practice comparisons, logicals, and f-strings