PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
PHY 3032 Chapter 1 - Introduction to Python 3 Fundamentals
Printing Numbers
The print() function outputs to the screen whatever you put between the parentheses.
In [1]: print(7)
In [2]: print(3.14)
3.14
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.
100
3.14
Printing very large numbers in Python 3 is mostly done using scientific notation .
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
In [7]: print(6.371e6)
6371000.0
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
In [11]: print(5.972e24)
5.972e+24
In [12]: print(7.34767309e22)
7.34767309e+22
In [13]: print(1.989e30)
1.989e+30
In [14]: print(0.00000458)
4.58e-06
In [15]: print(0.000_004_58)
4.58e-06
In [16]: print(4.58e-6)
4.58e-06
In [17]: print(1.6726219e-27)
1.6726219e-27
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.
In [19]: print("Hello")
Hello
In [20]: print('Hello')
Hello
In [21]: print('Hello")
In [22]: print("Hello')
Hello World
Hello World
Python as a Calculator
In [25]: print(7+9)
16
In [26]: print(7 + 9)
16
In [27]: print( 7 + 9 )
16
In [28]: print ( 7 + 9 )
16
In [29]: print( 20 - 8 )
12
In [30]: print( 8 - 20 )
-12
In [31]: print( 20 * 8 )
160
In [32]: print( 8 * 20 )
160
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
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 )
In [37]: print( 2 ** 3 )
In [38]: print( 5 ** 2 )
25
5.0
In [40]: print( 20 % 7 )
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.
In [42]: print( 3 % 10 )
In [43]: print( 6 % 20 )
In [44]: print( 1 + 2 * 3 )
In [45]: print( (1 + 2) * 3)
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 #
10
10
Create a bad comment that ends up commenting out the 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
In [53]: print(pi)
3.141592654
In [54]: print(radius)
---------------------------------------------------------------------
------
NameError Traceback (most recent call
last)
<ipython-input-54-eac3aa7f4c08> in <module>
----> 1 print(radius)
In [55]: radius = 10
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
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.
In [59]: print(radius)
5.5
Print out the new area of a circle using the values stored in pi and radius
95.03317778350001
In [62]: print(Area)
95.03317778350001
In [64]: print(first_name)
Mike
In [66]: print(surname)
Price
In [68]: print(age)
34
Multi-word variables can be created by connecting the words with an underscore _ character
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 .
In [72]: print(answer)
True
In [74]: print(answer)
False
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.
In [75]: type(pi)
Out[75]: float
In [76]: type(first_name)
Out[76]: str
In [77]: type(age)
Out[77]: int
In [78]: type(answer)
Out[78]: bool
String Concatenation
In Python 3, you can add strings using + sign. This is called string concatenation .
HelloWorld
Hello World
Hello World
Hello World
You can create strings, store them in variables and then concatenate them.
In [83]: print(first_name)
Mike
In [84]: print(surname)
Price
Mike Price
Create variable full_name that will store addition of surname and last_name
In [87]: print(full_name)
Mike Price
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.
In [89]: print(1 + 2)
In [90]: print("1" + 2)
---------------------------------------------------------------------
------
TypeError Traceback (most recent call
last)
<ipython-input-90-2c8b8ec3d2f2> in <module>
----> 1 print("1" + 2)
---------------------------------------------------------------------
------
TypeError Traceback (most recent call
last)
<ipython-input-91-f5a1f3a83b1d> in <module>
----> 1 print(1 + "2")
12
To add a number to a string, you must first convert the number to a string using str() function.
12
12
12
In [96]: print(age)
34
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
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.")
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.")
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.
In [101]: year
Out[101]: '2021'
In [102]: type(year)
Out[102]: str
In [103]: int(year)
Out[103]: 2021
In [104]: type(int(year))
Out[104]: int
In [105]: type(year)
Out[105]: str
In [106]: year
Out[106]: '2021'
In [108]: year
Out[108]: 2021
In [109]: type(year)
Out[109]: int
In [111]: print(disease)
covid-19
In [112]: print(disease * 3)
covid-19covid-19covid-19
You can not repeat a string by multiplying the string with a float.
---------------------------------------------------------------------
------
TypeError Traceback (most recent call
last)
<ipython-input-113-bfc40f76428e> in <module>
----> 1 print(disease * 3.5)
In [ ]: