21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Printing Numbers
The print() function outputs to the screen whatever you put between the parentheses.
Print the integer 7 .
In [1]: print(7)
Print the floating point number 3.14
In [2]: print(3.14)
3.14
You can have more than one print() statement in a cell.
Print out numbers 100 and 3.14 using different print() functions on different lines in the same cell.
In [3]: print(100)
print(3.14)
100
3.14
Print out numbers 100 and 3.14 using different print() functions on the same line in the same cell.
In [4]: print(100) ; print(3.14)
100
3.14
Printing very large numbers in Python 3 is mostly done using scientific notation .
Print out radius of earth : 6371000 meters.
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 1/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [5]: print(6371000)
6371000
Print out radius of earth : 6 371 000 meters using underscores to increase readability.
In [6]: print(6_371_000)
6371000
Print out radius of earth : 6.371 × 106 meters in scientific notation.
In [7]: print(6.371e6)
6371000.0
Print out earth sun distance : 149600000000 meters.
In [8]: print(149600000000)
149600000000
Print out earth sun distance : 149 600 000 000 meters using underscores to improve readability.
In [9]: print(149_600_000_000)
149600000000
Print out earth sun distance : 1.496 × 1011 meters in scientific notation.
In [10]: print(1.496e11)
149600000000.0
Print out mass of earth : 5.972 × 10
24
kg in scientific notation.
In [11]: print(5.972e24)
5.972e+24
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 2/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Print out mass of our moon : 7.34767309 × 10
22
kg in scientific notation.
In [12]: print(7.34767309e22)
7.34767309e+22
Print out mass of our sun : 1.989 × 1030 kg in scientific notation.
In [13]: print(1.989e30)
1.989e+30
Printing small numbers in Python 3.
Print out 0.00000458
In [14]: print(0.00000458)
4.58e-06
Print out 0.000 004 58
In [15]: print(0.000_004_58)
4.58e-06
Print out 4.58 × 10−6 in scientific notation
In [16]: print(4.58e-6)
4.58e-06
Print out proton mass : 1.6726219 × 10−27 in kg
In [17]: print(1.6726219e-27)
1.6726219e-27
Print out electron mass : 9.1093837015 × 10
−31
in kg
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 3/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [18]: print(9.1093837015e-31)
9.1093837015e-31
Printing Strings
A string is a collection of characters. To print strings in Python 3, you need to enclose the string or text in
single or double quotation marks.
Print the string "Hello"
In [19]: print("Hello")
Hello
Print the string 'Hello'
In [20]: print('Hello')
Hello
Printing a string with unmatched quotation marks generates a SyntaxError .
In [21]: print('Hello")
File "<ipython-input-21-0779651877b1>", line 1
print('Hello")
^
SyntaxError: EOL while scanning string literal
In [22]: print("Hello')
File "<ipython-input-22-21f0c5b97628>", line 1
print("Hello')
^
SyntaxError: EOL while scanning string literal
Printing a multi-word string "Hello World" .
In [23]: print("Hello World")
Hello World
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 4/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Printing a multi-word string 'Hello World' .
In [24]: print('Hello World')
Hello World
Python as a Calculator
To add two numbers, just use + operator.
In [25]: print(7+9)
16
Python 3 ignores whitespaces . Whitespaces make Python code more readable.
In [26]: print(7 + 9)
16
In [27]: print( 7 + 9 )
16
In [28]: print ( 7 + 9 )
16
You can use the - sign to subtract numbers.
In [29]: print( 20 - 8 )
12
In [30]: print( 8 - 20 )
-12
You can use the * sign to multiply two or more numbers.
In [31]: print( 20 * 8 )
160
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 5/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [32]: print( 8 * 20 )
160
Python 3 understands associativity in multiplication.
In [33]: print( 2 * 3 * 5 )
print( 3 * 2 * 5 )
print( 2 * 5 * 3 )
print( 5 * 2 * 3 )
print( 5 * 3 * 2 )
30
30
30
30
30
To divide two numbers, use / sign.
In [34]: print( 20 / 5 )
print( 5 / 20 )
print( -20 / 4 )
print( -18 / -6 )
4.0
0.25
-5.0
3.0
You can carry out integer division using // sign. The decimal part is discarded
In [35]: print( 20 / 7 )
2.857142857142857
In [36]: print( 20 // 7 )
You use the ** to use a number to the power of another number.
Print out 2 raised to the power 3 .
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 6/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [37]: print( 2 ** 3 )
Print out 5 raised to the power 2 .
In [38]: print( 5 ** 2 )
25
Print out 25 raised to the power 0.5 .
In [39]: print( 25 ** 0.5 )
5.0
To obtain the remainder of a division operation , use the the % sign.
Print out the remainder of 20 divide by 7 .
In [40]: print( 20 % 7 )
Print out the remainder of 10 divide by 3 .
In [41]: print( 10 % 3 )
The remainder operation gives trivial results when you divide a small number by a big number. The remainder is
always the small number.
Print out the remainder of 3 divide by 10 .
In [42]: print( 3 % 10 )
Print out the remainder of 6 divide by 20 .
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 7/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [43]: print( 6 % 20 )
Like on a calculator, parentheses () can be used to priotize calculations.
In [44]: print( 1 + 2 * 3 )
In [45]: print( (1 + 2) * 3)
Parentheses can also be used to improve the readability of Python code.
In [46]: print( 1 + (2 * 3) )
Comments
Python 3 ignores lines of code starting with a # sign, so they can be used to add notes or comments to the
code.
A # can also be used in the middle of a line. In this case, Python ignores everything on the line after the #
Create a Python 3 comment
In [47]: # This is a comment
Create a comment followed by Python 3 code
In [48]: # This is a good comment
print( 3 + 7 )
10
Create a comment lined side by side with Python 3 code
In [49]: print( 3 + 7 ) # This is a good comment
10
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 8/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Create a bad comment that ends up commenting out the Python 3 code
In [50]: # This is a bad comment print( 3 + 7 )
Create a misleading comment that comes after Python 3 code
In [51]: print( 3 + 7 )
# This is a misleading comment
10
Variables
Variables are used to store values for later use. Storing a value in a variable is called variable
declaration or variable assignment . You must declare a variable in Python 3 before you can use its
value.
Create a variable pi
In [52]: pi = 3.141592654
Print out variable pi
In [53]: print(pi)
3.141592654
Trying to use a variable before it is given a value, results in a Python 3 NameError .
Print out variable radius .
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 9/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [54]: print(radius)
---------------------------------------------------------------------
------
NameError Traceback (most recent call
last)
<ipython-input-54-eac3aa7f4c08> in <module>
----> 1 print(radius)
NameError: name 'radius' is not defined
Create variable radius and assign it a value of 10 .
In [55]: radius = 10
Print out variable radius .
In [56]: print(radius)
10
Variables can be used in calculations. The value of the variable will be used for the compuation of the result.
Print out the area of a circle using the values stored in pi and radius
In [57]: print( pi * radius ** 2)
314.1592654
The value of a variable can be changed. This is called a variable reassignment . When a variable is
reassigned, its previous value is overwritten or lost.
Store a value of 5.5 in variable radius .
In [58]: radius = 5.5
Print out the new value stored in variable radius .
In [59]: print(radius)
5.5
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 10/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Print out the new area of a circle using the values stored in pi and radius
In [60]: print( pi * radius ** 2)
95.03317778350001
Store area in Area
In [61]: Area = pi * radius ** 2
Print out variable Area
In [62]: print(Area)
95.03317778350001
Variables can also store string and integer values.
Create a variable first_name
In [63]: first_name = "Mike"
Print out variable first_name .
In [64]: print(first_name)
Mike
Create a variable surname
In [65]: surname = "Price"
Print out variable surname .
In [66]: print(surname)
Price
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 11/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Create variable age .
In [67]: age = 34 # in years
Print out variable age .
In [68]: print(age)
34
Multi-word variables can be created by connecting the words with an underscore _ character
Create a variable electron_mass in units of kilograms
In [69]: electron_mass = 9.10938356e-31 # in kg
Print out variable electron_mass .
In [70]: print(electron_mass)
9.10938356e-31
Variables can store True or False values. In Python 3, True or False values are called Booleans .
Create variable answer holding a value of True
In [71]: answer = True
Print out variable answer .
In [72]: print(answer)
True
Re-assign variable answer with False .
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 12/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [73]: answer = False
Print out variable answer .
In [74]: print(answer)
False
Basic Data Types in Python 3
Data types are how you classify variables in Python 3. The basic data type available in Python 3 are
integers , floats , strings and booleans .
integers
An integer (abbr. int ) is a whole number (no decimal places)
floats
Floats (abbr. float ) are numbers with decimal places
strings
In Python non-numbers like letters or words are called strings (abbr. str )
booleans
in Python 3, True or False are known as Booleans ( abbr. bool ). Booleans represent truthness and
falseness. The capitalization is important.
Checking the data type of a variable
You can use the type() function to get the data type of a variable.
Get the data type of variable pi .
In [75]: type(pi)
Out[75]: float
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 13/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Get the data type of variable first_name .
In [76]: type(first_name)
Out[76]: str
Get the data type of variable age .
In [77]: type(age)
Out[77]: int
Get the data type of variable answer .
In [78]: type(answer)
Out[78]: bool
String Concatenation
In Python 3, you can add strings using + sign. This is called string concatenation .
Add "Hello" and "World"
In [79]: print("Hello" + "World")
HelloWorld
Add "Hello " and "World"
In [80]: print("Hello " + "World")
Hello World
Add "Hello" and " World"
In [81]: print("Hello" + " World")
Hello World
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 14/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Another way of adding "Hello" and "World"
In [82]: print("Hello" + " " + "World") # best practise
Hello World
You can create strings, store them in variables and then concatenate them.
In [83]: print(first_name)
Mike
Print variable surname
In [84]: print(surname)
Price
Print out addition of first_name and surname .
In [85]: print(first_name + " " + surname)
Mike Price
Create variable full_name that will store addition of surname and last_name
In [86]: full_name = first_name + " " + surname
Print out full_name .
In [87]: print(full_name)
Mike Price
Print out addition of string "My name is " and full_name
In [88]: print("My name is " + full_name + ".")
My name is Mike Price.
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 15/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Data Type Conversion i.e. Casting
Python 3 does not allow the addition of string and number without casting . Trying to add strings and integers
generates a TypeError .
In Python 3, you can cast numbers into strings by enclosing the number in quotation marks. The numbers
enclosed in quotation marks will be treated as strings.
Print the addition of numbers 1 and 2
In [89]: print(1 + 2)
Print the addition of numbers "1" and 2
In [90]: print("1" + 2)
---------------------------------------------------------------------
------
TypeError Traceback (most recent call
last)
<ipython-input-90-2c8b8ec3d2f2> in <module>
----> 1 print("1" + 2)
TypeError: can only concatenate str (not "int") to str
Print the addition of numbers 1 and "2"
In [91]: print(1 + "2")
---------------------------------------------------------------------
------
TypeError Traceback (most recent call
last)
<ipython-input-91-f5a1f3a83b1d> in <module>
----> 1 print(1 + "2")
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Print the addition of the numeric strings "1" and "2"
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 16/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [92]: print( "1" + "2" )
12
To add a number to a string, you must first convert the number to a string using str() function.
Print the addition of numbers 1 and 2 using the str() .
In [93]: print( "1" + str(2) )
12
In [94]: print( str(1) + "2" )
12
In [95]: print( str(1) + str(2) )
12
Print out the variable age
In [96]: print(age)
34
Get the data type of variable age
In [97]: type(age)
Out[97]: int
Try to print out the string "My name is " + full_name + "." + " I am " + age + " years
old." without first converting age into a string
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 17/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [98]: print("My name is " + full_name + "." + " I am " + age + " years ol
d.")
---------------------------------------------------------------------
------
TypeError Traceback (most recent call
last)
<ipython-input-98-2fcebf0bdf01> in <module>
----> 1 print("My name is " + full_name + "." + " I am " + age + " ye
ars old.")
TypeError: can only concatenate str (not "int") to str
Print out the string "My name is " + full_name + "." + " I am " + str(age) + " years
old."
In [99]: print("My name is " + full_name + "." + " I am " + str(age) + " years
old.")
My name is Mike Price. I am 34 years old.
You can also convert a numeric string (i.e. a number in quotation marks) into integer using int()
function or a float using float() function.
Create string variable year
In [100]: year = "2021"
Display the variable year
In [101]: year
Out[101]: '2021'
Get the data type of variable year .
In [102]: type(year)
Out[102]: str
Covert year into an integer with help of int()
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 18/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
In [103]: int(year)
Out[103]: 2021
Get the data type int(year)
In [104]: type(int(year))
Out[104]: int
Get the data type of variable year .
In [105]: type(year)
Out[105]: str
Display the variable year
In [106]: year
Out[106]: '2021'
Permanently change the data type of year from string to integer
In [107]: year = int(year)
Display out year again.
In [108]: year
Out[108]: 2021
Get the data type of year .
In [109]: type(year)
Out[109]: int
You can repeat a string by multiplying the string by an integer.
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 19/20
21/01/2021 PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Create a variable disease .
In [110]: disease = "covid-19"
Display variable disease .
In [111]: print(disease)
covid-19
Multiply variable disease by 3 .
In [112]: print(disease * 3)
covid-19covid-19covid-19
You can not repeat a string by multiplying the string with a float.
Multiply variable disease by 3.5
In [113]: print(disease * 3.5)
---------------------------------------------------------------------
------
TypeError Traceback (most recent call
last)
<ipython-input-113-bfc40f76428e> in <module>
----> 1 print(disease * 3.5)
TypeError: can't multiply sequence by non-int of type 'float'
In [ ]:
localhost:8891/nbconvert/html/PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals.ipynb?download=false 20/20