Python fundamentals

Author

Harison Gachuru

Published

February 22, 2023

Open In Colab

Data types

Integers

type(100)
int

Floating-point numbers

type(3.14)
float

Strings

type("Hello world!")
str

Boolean

type(True)
bool

Lists

type(["Audi", "BMW", "Mercedes"])
list

Tuples

type(("SUV", "saloon/sedan", "hatchback"))
tuple

Dictionaries

type({"make": "Suzuki", "model": "Alto"})
dict

Sets

type({1, 2, 3, 5, 7})
set

Math operators

Addition

3 + 5
8

Subtraction

100 - 20
80

Multiplication

2 * 5
10

Division

100 / 2
50.0

Integer division/floored quotient

23 // 4
5

Modulus/remainder

23 % 4
3

Exponentiation

10 ** 2
100
(2 ** .5)**2
2.0000000000000004

String operators

Concatenation

"Hello" + " " + "world" + "!"
'Hello world!'

Replication

"Hello world!" * 3
'Hello world!Hello world!Hello world!'
print("=" * 50 + "\n" + "Hello world!" + "\n" + "=" * 50)
==================================================
Hello world!
==================================================

Variables

x = 5
x
5
print("x is " + str(x))
x is 5
text = "Hello world!"
text
'Hello world!'
name = input("Enter your name:\n")
print("Hello " + name + "!")
Enter your name:
Harison
Hello Harison!
name = input("Enter your name:\n")
template = "*" * 50
print(f"{template}\nHello {name}!\n{template}")
Enter your name:

**************************************************
Hello !
**************************************************

Question: What happens when we reference an unassigned variable?

Reading and writing files

file_ref = open("/content/sample_data/README.md")
print(file_ref.read())
file_ref.close()
This directory includes a few sample datasets to get you started.

*   `california_housing_data*.csv` is California housing data from the 1990 US
    Census; more information is available at:
    https://developers.google.com/machine-learning/crash-course/california-housing-data-description

*   `mnist_*.csv` is a small sample of the
    [MNIST database](https://en.wikipedia.org/wiki/MNIST_database), which is
    described at: http://yann.lecun.com/exdb/mnist/

*   `anscombe.json` contains a copy of
    [Anscombe's quartet](https://en.wikipedia.org/wiki/Anscombe%27s_quartet); it
    was originally described in

    Anscombe, F. J. (1973). 'Graphs in Statistical Analysis'. American
    Statistician. 27 (1): 17-21. JSTOR 2682899.

    and our copy was prepared by the
    [vega_datasets library](https://github.com/altair-viz/vega_datasets/blob/4f67bdaad10f45e3549984e17e1b3088c731503d/vega_datasets/_data/anscombe.json).
with open("/content/sample_data/README.md") as file:
    print(file.read())
This directory includes a few sample datasets to get you started.

*   `california_housing_data*.csv` is California housing data from the 1990 US
    Census; more information is available at:
    https://developers.google.com/machine-learning/crash-course/california-housing-data-description

*   `mnist_*.csv` is a small sample of the
    [MNIST database](https://en.wikipedia.org/wiki/MNIST_database), which is
    described at: http://yann.lecun.com/exdb/mnist/

*   `anscombe.json` contains a copy of
    [Anscombe's quartet](https://en.wikipedia.org/wiki/Anscombe%27s_quartet); it
    was originally described in

    Anscombe, F. J. (1973). 'Graphs in Statistical Analysis'. American
    Statistician. 27 (1): 17-21. JSTOR 2682899.

    and our copy was prepared by the
    [vega_datasets library](https://github.com/altair-viz/vega_datasets/blob/4f67bdaad10f45e3549984e17e1b3088c731503d/vega_datasets/_data/anscombe.json).
?open
file_ref = open("example.txt", "w")
file_ref.write("The quick brown fox jumps over the lazy dog")
file_ref.close()
with open("example.txt") as file:
    print(file.read())
The quick brown fox jumps over the lazy dog
with open("example2.txt", mode="w") as file:
    file.write("Some text")

Error handling

Imports

Iteration

Flow control