Integer

2 snippets across 2 stacks - Python, SQL

Also written as to integer

PYPython

Integers & Floats

PY · Numbers
Syntax
x = 42      # int
y = 3.14    # float
Example
count = 1_000_000
ratio = 0.618
print(type(count), type(ratio))
print(count + ratio)
Output
<class 'int'> <class 'float'>
1000000.618

Note Underscores in numeric literals are ignored and serve as visual separators. Python ints have unlimited precision.

SQLSQL

CAST / Type Conversion

SQL · String Functions
Syntax
CAST(expression AS data_type)
expression::data_type  -- PostgreSQL shorthand
Example
SELECT
  CAST(price AS INTEGER) AS rounded_price,
  CAST(order_date AS VARCHAR) AS date_string,
  '42'::INTEGER + 8 AS sum_pg  -- PostgreSQL only
FROM products;
Output
-- rounded_price | date_string | sum_pg
-- 29             | 2025-03-15  | 50

Note CAST is ANSI standard. PostgreSQL's :: shorthand is shorter but not portable. Be careful casting - CAST('abc' AS INTEGER) will throw an error. Use TRY_CAST in SQL Server for safe conversions.

Frequently asked questions

How does Python handle integer?
This task is covered in 2 stacks on this page: Python, SQL. The "Integers & Floats" snippet in Python uses `x = 42 # int`.
Which code does the Python example use?
The "Integers & Floats" snippet uses `x = 42 # int`, from the Numbers section of the Python cheat sheet.
Which stacks cover "integer" on this page?
Python, SQL. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Integers & Floats": Underscores in numeric literals are ignored and serve as visual separators. Python ints have unlimited precision.