Syntax and Types

2. Syntax and Types#

This chapter builds the basic vocabulary of Python programs: names, values, operators, and types.

  • Variables and assignment

  • Operators and expressions

  • Basic scalar types

  • Collection type overview

Chapter Video

This overview introduces numeric data, arithmetic operators, comparisons, and basic type conversion.

Python Tutorial for Beginners 3: Integers and Floats - Working with Numeric Data (11:55) by Corey Schafer

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Create and name variables using Python assignment syntax

  2. Use arithmetic, comparison, logical, and assignment operators

  3. Classify values by built-in type

  4. Convert values between compatible built-in types

  5. Choose appropriate basic and collection types for simple data

Chapter Flow

Glossary

#

Term

Explanation

1

Variable

A name that refers to a value or object in a program.

2

Assignment statement

A statement that binds a variable name to a value, such as score = 95.

3

Naming Rules & Conventions

Shared rules and recommended styles for variable names and other identifiers.

4

Object

A Python value with an identity, a type, and data.

5

Identity

The unique object reference Python uses to tell whether two names point to the same object.

6

Alias

A second name that refers to the same object as another name.

7

Type

The category of a value, such as int, float, str, bool, list, or dict.

8

Operator

A symbol or word that performs an operation, such as +, ==, and, or, or in.

9

Operand

A value that an operator works on.

10

Expression

A piece of code that evaluates to a value.

11

Type conversion

The process of changing a value from one type to another.

12

Type hint

An annotation that records the expected type of a variable, parameter, or return value.

13

Mutable

Describes an object whose contents can be changed after creation.

14

Immutable

Describes an object whose contents cannot be changed after creation.

15

List

An ordered, mutable collection of values.

16

Tuple

An ordered, immutable collection of values.

17

Dictionary

A mutable collection that stores key-value pairs.

18

Set

A mutable collection that stores unique values without a fixed order.

19

Boolean

A type with two possible values: True and False.

20

Truthy

Describes a value that behaves like True in a Boolean context.

21

Falsy

Describes a value that behaves like False in a Boolean context.

22

Reassignment

Binding an existing variable name to a different object; the old object itself is not changed.

23

Mutation

Changing the contents of an object in place, such as appending to a list, so every name that refers to it sees the change.

24

Dynamic typing

Python’s approach of determining a variable’s type at run time from the value assigned, so you do not declare types.

25

Floor division

The // operator, which divides two numbers and rounds the result down to the nearest integer.

26

Modulus operator

The % operator, which returns the remainder after division.

27

Membership operator

The in or not in operator, which checks whether a value is present in a collection.

28

Short-circuit evaluation

Evaluation of and and or that stops as soon as the result is known, so the second operand may never be evaluated.

29

Operator precedence

The rules that decide which operations in an expression are evaluated first, such as ** before +.

30

Type casting

Explicit conversion that you perform by calling a type constructor, such as int("42").

31

Type coercion

Implicit conversion that Python performs automatically, such as treating 3 as 3.0 in 3 + 4.5.

32

String

An immutable sequence of characters written in quotes, such as "apple".

33

Concatenation

Joining strings end to end with the + operator.

34

Escape sequence

A backslash followed by a character, such as \n or \', that represents a special character inside a string.

35

Indexing

Accessing one item of a sequence by its position in square brackets, starting from 0, such as s[0].

36

Slicing

Extracting part of a sequence with [start:stop:step], where the item at the stop index is not included.

Chapter Overview Slides