Data Types & I/O#
Commonly-used data types (with Python examples) include:
Name | Python Syntax | Example | Description |
---|---|---|---|
String | str() | "Hello World!" | String of characters |
Integer | int() | 7 | Whole number |
Float | float() | 1.5 | Decimal number, or number with floating point values |
Boolean | bool() | True | Two-state system that typically represents true/false values |
Nothing/Null/NoneType | NoneType | None | No value |
For more on data types in Python:
Numbers#
An integer is a whole number that does not include any decimal (or fractional) values. A float data type includes decimal (or fractional) values.
# printing an integer
print(3)
# storing an integer to a variable
number = 3
# show number variable type
print(type(number))
# print number variable
print(number)
# printing a decimal or float value
print(3.5)
# store float value to a variable
number = 3.5
# show number variable type
print(type(number))
# print number variable
print(number)
Strings#
A string is a sequence of characters (letters, numbers, symbols, etc.) We can assign strings to a variable (i.e. store them as a variable).
# assign string to variable
s = "Hello world!"
# show variable type
print(type(s))
# print variable
print(s)
<class 'str'>
Hello world!
Input & Output (I/O)#
In programming languages and computing more broadly, I/O
stands for Input
and Output
. Programming languages can take a variety of inputs (user-provided values, data, files, etc) and return outputs in a variety of formats (data stored in memory, output that shows up in the console, newly-created or -modified files, etc).
We’ve already seen I/O
in action in Python via print()
statements. One way Python accepts input is via the input()
function, which accepts a user input and assigns that to a variable.
A sample program that asks a user to enter their name:
# initial prompt
print("Enter your name: ")
# input function
name = input()
# output statement
print("Hello, " + name)
We can modify that program to include the initial prompt as part of the input()
statement.
# initial prompt with input function
name = input("Enter your name: ")
# output
print("Hello, " + name)
NOTE: The default data type for a variable created using the input()
function is a string object.
For more background on I/O in Python: