Lecture 3: Data Types, Input/Output, and Basic Functions
Data Types
Memory stores data as 0s and 1s; a variable describes a memory area; the variable’s type determines how to interpret that data.
Standard built-in types: Integer, Floating-Point, Booleans, Strings.
Python uses dynamic typing (type inferred from creation/assignment).
Integers
- Whole numbers, positive or negative; examples: 1, 2, 0, -20.
Floating-Point Numbers
- Numbers with a decimal point; examples: 1.01, 2.0, -20.05, 0.004.
- Often thought of in terms of Scientific Notation: Mantissa and Exponent.
- Example: 1234.567 = 1.234567 \times 10^{3}
Booleans
- Values: True or False.
Strings
- Text data; defined with quotes (single or double). E.g., "This is a string" or 'So is this'.
- To include quotes inside a string, use escaping (backslash) or triple-quote strings.
Type Conversions
General form: new_type(value)
Integers
- int → drop fractional part when converting to int (truncation): int(3.14) → 3
- float → 3.0 for float(3) and 3.0 for 3 converted to float: float(3) → 3.0
Strings to numbers
- int('3') → 3; float('3.14') → 3.14
- int('2.5') is an error; use int(float('2.5')) → 2
- float('2') → 2.0
Numbers to strings
- str(2.5) → '2.5'
- repr vs str: repr may show quotes/newlines; str is for display
Booleans
- True → 1, False → 0 when converted to numbers
- bool(0) → False; bool(3) → True; bool('') → False; bool('0') → True
Operations and Type Behavior
- + operator
- Numbers: addition; 1 + 2 = 3
- Strings: concatenation; "1" + "2" = "12"
- Other operations on strings
- - and / not defined for strings; * supports repetition with an int
- Division
- / yields a float; // is floor division; 2/2 → 1.0, 2//2 → 1
Printing and Output
print(x) writes to console; ends with a newline; multiple values separated by spaces
Example: print(2.0, 'is', 2) → 2.0 is 2
Customizing print
- sep: print(a, b, sep=',') → a,b
- end: print(a, b, end=' : ') to avoid a newline
f-strings for formatting
- print(f"Test {3} {5}") → Test 3 5
- {value:.Nf} formats a number with N digits after the decimal
Formatting alignment (strings/numbers)
- f'{2.3:<10}' → '2.3 '
- f'{2.3:>10}' → ' 2.3'
- f'{2.3:^10}' → ' 2.3 '
Example: formatting a value with pi
- from math import *
- r = float(input("Enter the radius of a circle: "))
- area = pi * r ** 2
- print(f"The area of the circle is {area:.4f}")
- This prints area to 4 decimal places, e.g. 3.1416 when r = 1
Area formula (math reference)
- Area of a circle: A = \pi r^2
Input
- input() reads a line from standard input and returns a string
- To use as a number, convert: age = int(input()) or r = float(input())
- Prompting the user
- print("Enter the radius:") then r = float(input())
- Or inline prompt: input("Enter the radius of a circle: ")
- Example: compute circle area with input radius and optional prompt formatting
Functions
- Functions are named blocks of code reusable via calls
- Function calls: name(args)
- Definition syntax:
- def function_name(parameters):
- Indentation is significant (commonly 4 spaces per level)
- def function_name(parameters):
- Example: areaofcircle(radius)
- area = pi * radius ** 2
- return area
- Example usage with input/output
- r = float(input("Enter the radius of circle 1: "))
- print(f"The area of circle 1 is {areaofcircle(r):.4f}")
Why use functions
- Breaks complex tasks into smaller parts; enables reuse and better organization