The easiest way to install Python is to use brew
.
$ brew install python3
Print your installed Python's version.
$ python3 --version
We can read-evaluate-print-loop (REPL) Python code in real-time.
$ python3
>>> print("Hello World!")
Hello World!
REPL can be closed by following code.
$ python3
>>> exit()
A variable is a reference to a location in memory that holds a value. Think of it as a labeled box that you can put things in and out of it so that you can easily refer to it by using its label.
name = "Nicholas"
height = 170
Python has four main basic data types. They are strings, integers, floats, and Boolean.
A string is a group of characters.
"GoLife for life"
'This is also a string'
"It's sunny outside"
You can concatenate string together by using the +
operator.
"It's snowing" + " " + "in Bali!"
You can access individual characters by using the subscript operator. The subscript operator takes an integer that represents the position of the character in the string. This is called the index. Indices start from 0.
"What a wonderful world!"[5]
An integer is a whole number. Integers can be positive or negative.
42
-1
10000
0
You can use math operators to do additions, subtractions, multiplications, and divisions.
1 + 10
-1 - 5
10 * 100
10 / 2
10 // 2
The //
is what's called a "floor" division operator. It will simply remove any decimal digits resulting from the division.
A floating-point number is basically a decimal number. Just like integers, floats can also be positive or negative.
3.14
1e2
-2.0
You can also use math operators to do additions, subtractions, multiplications, and divisions.
2.5 + 2.5
1.3 - 0.3
2 * 2.7
3.3 / 3
3.3 // 3
A Boolean is a data type that represents either True
or False
.
True
False
You can use the and
, or
, and not
operators to manipulate Boolean values
True and False
True or False
not True
You also can use the ==
, !=
, <=
, and >=
operators to manipulate Boolean values
1 == 2
True != False
10 >= 0
2 <= 10
Use the type
function to figure out the type of a value or variable
type("this is a string")
type(9001)
type(3.14)
type(False)
You can ask for input by using the input
function. You can store the input inside a variable by using the =
operator.
input()
name = input("What is your name? ")
You can print things to the screen by using the print
function. You can print a variable's value by passing the variable as an argument to the print
function.
print()
print(name)
You can put your Python code into a file which you can then later run. Open a text editor and type in the following.
name = input("What is your name? ")
print("Hello, " + name)
Save the file as name.py
and run it.
$ python3 name.py
-
Consider the following code
x = 1 y = x y = 2
What is the final value of variable
x
andy
? Explain the answer using your own words! -
Consider the following code
"Wonderful" - "world"
What happens when you run the above code? Explain the answer using your own words!
-
Consider the following code
name = "Donald Trump" print(name[8:-1])
What does the above code print? Explain the answer using your own words!
-
Write a Python script that asks for two numbers. Add, subtract, multiply, divide the two numbers together and print them to the screen. Save the Python script as
calculate.py
. Your program's behavior should match the example below.$ python3 calculate.py First number: 2 Second number: 5 2 + 5 = 7 2 - 5 = -3 2 * 5 = 10 2 / 5 = 0.4 2 // 5 = 0
-
Write a Python script that will take a side of a square as an argument from the command line. Calculate its Area and Perimeter and print them to the screen. Your program's behavior should match the example below.
$ python3 square.py 3 Area = 9 Perimeter = 12