0% found this document useful (0 votes)
50 views16 pages

First Python Coursera Kulsoom

Uploaded by

Kulsoom Shakeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views16 pages

First Python Coursera Kulsoom

Uploaded by

Kulsoom Shakeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

First_Python_Coursera_Kulsoom

January 16, 2023

1 Writing Your First Python Code

Estimated time needed: 25 minutes

1.1 Objectives

After completing this lab you will be able to:


• Write basic code in Python
• Work with various types of data in Python
• Convert the data from one type to another
• Use expressions and variables to perform operations
Table of Contents
<ul>
<li>
<a href="https://#hello">Say "Hello" to the world in Python</a>
<ul>
<li><a href="https://version/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_c
<li><a href="https://comments/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_
<li><a href="https://errors/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_co
<li><a href="https://python_error/?utm_medium=Exinfluencer&utm_source=Exinfluencer&
<li><a href="https://exercise/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_
</ul>
</li>
<li>
<a href="https://#types_objects">Types of objects in Python</a>
<ul>
<li><a href="https://int/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_conte
<li><a href="https://float/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_con
<li><a href="https://convert/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_c
<li><a href="https://bool/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_cont
<li><a href="https://exer_type/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm
</ul>
</li>
<li>
<a href="https://#https://exp/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_cont

1
<ul>
<li><a href="exp">Expressions</a></li>
<li><a href="https://exer_exp/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_
<li><a href="https://var/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_conte
<li><a href="https://exer_exp_var/?utm_medium=Exinfluencer&utm_source=Exinfluencer&
</ul>
</li>
</ul>
<p>
Estimated time needed: <strong>25 min</strong>
</p>
Say “Hello” to the world in Python
When learning a new programming language, it is customary to start with an “hello world” example.
As simple as it is, this one line of code will ensure that we know how to print a string in output
and how to execute code within cells in a notebook.
[1]: # Try your first Python output

print('Hello, Python!')

print('Hello, Python!')

Hello, Python!
Hello, Python!
After executing the cell above, you should see that Python prints Hello, Python!. Congratulations
on running your first Python code!
[Tip:] <code>print()</code> is a function. You passed the string <code>'Hello, Python!'</code>
What version of Python are we using?
There are two popular versions of the Python programming language in use today: Python 2 and
Python 3. The Python community has decided to move on from Python 2 to Python 3, and many
popular libraries have announced that they will no longer support Python 2.
Since Python 3 is the future, in this course we will be using it exclusively. How do we know that
our notebook is executed by a Python 3 runtime? We can look in the top-right hand corner of this
notebook and see “Python 3”.
We can also ask Python directly and obtain a detailed answer. Try executing the following code:
[2]: # Check the Python Version

import sys
print(sys.version)

3.7.6 | packaged by conda-forge | (default, Mar 23 2020, 23:03:20)


[GCC 7.3.0]
[Tip:] <code>sys</code> is a built-in module that contains many system-specific parameters and

2
Writing comments in Python
In addition to writing code, note that it’s always a good idea to add comments to your code. It
will help others understand what you were trying to accomplish (the reason why you wrote a given
snippet of code). Not only does this help other people understand your code, it can also serve as a
reminder to you when you come back to it weeks or months later.
To write comments in Python, use the number symbol # before writing your comment. When you
run your code, Python will ignore everything past the # on a given line.
[5]: # Practice on writing comments

print('Hello, Python!') # This line prints a string


print('Hi')

Hello, Python!
Hi
After executing the cell above, you should notice that This line prints a string did not appear in
the output, because it was a comment (and thus ignored by Python).
The second line was also not executed because print(‘Hi’) was preceded by the number sign (#) as
well! Since this isn’t an explanatory comment from the programmer, but an actual line of code, we
might say that the programmer commented out that second line of code.
Errors in Python
Everyone makes mistakes. For many types of mistakes, Python will tell you that you have made a
mistake by giving you an error message. It is important to read error messages carefully to really
understand where you made a mistake and how you may go about correcting it.
For example, if you spell print as frint, Python will display an error message. Give it a try:
[6]: # Print string as error message

frint("Hello, Python!")


,→---------------------------------------------------------------------------

NameError Traceback (most recent call␣


,→last)

<ipython-input-6-313a1769a8a5> in <module>
1 # Print string as error message
2
----> 3 frint("Hello, Python!")

NameError: name 'frint' is not defined

3
The error message tells you:
where the error occurred (more useful in large notebook cells or scripts), and
what kind of error it was (NameError)
Here, Python attempted to run the function frint, but could not determine what frint is since it’s
not a built-in function and it has not been previously defined by us either.
You’ll notice that if we make a different type of mistake, by forgetting to close the string, we’ll
obtain a different error (i.e., a SyntaxError). Try it below:

[7]: # Try to see built-in error message

print("Hello, Python!)

File "<ipython-input-7-f0b5a635e1a2>", line 3


print("Hello, Python!)
^
SyntaxError: EOL while scanning string literal

Does Python know about your error before it runs your code?
Python is what is called an interpreted language. Compiled languages examine your entire program
at compile time, and are able to warn you about a whole class of errors prior to execution. In
contrast, Python interprets your script line by line as it executes it. Python will stop executing
the entire program when it encounters an error (unless the error is expected and handled by the
programmer, a more advanced subject that we’ll cover later on in this course).
Try to run the code in the cell below and see what happens:
[8]: # Print string and error to see the running order

print("This will be printed")


frint("This will cause an error")
print("This will NOT be printed")

This will be printed


,→---------------------------------------------------------------------------

NameError Traceback (most recent call␣


last)
,→

<ipython-input-8-af59af1b345d> in <module>

4
2
3 print("This will be printed")
----> 4 frint("This will cause an error")
5 print("This will NOT be printed")

NameError: name 'frint' is not defined

Exercise: Your First Program


Generations of programmers have started their coding careers by simply printing “Hello, world!”.
You will be following in their footsteps.
In the code cell below, use the print() function to print out the phrase: Hello, world!

[9]: # Write your code below. Don't forget to press Shift+Enter to execute the cell
print("Hello, world!")

Hello, world!
Click here for the solution
print("Hello, world!")
Now, let’s enhance your code with a comment. In the code cell below, print out the phrase: Hello,
world! and comment it with the phrase Print the traditional hello world all in one line of code.
[10]: # Write your code below. Don't forget to press Shift+Enter to execute the cell
print("Hello, world!") #Print the traditional hello world

Hello, world!
Click here for the solution
print("Hello, world!") # Print the traditional hello world
Types of objects in Python
Python is an object-oriented language. There are many different types of objects in Python. Let’s
start with the most common object types: strings, integers and floats. Anytime you write words
(text) in Python, you’re using character strings (strings for short). The most common numbers,
on the other hand, are integers (e.g. -1, 0, 100) and floats, which represent real numbers (e.g. 3.14,
-42.0).
The following code cells contain some examples.
[11]: # Integer

11

[11]: 11

5
[12]: # Float

2.14

[12]: 2.14

[13]: # String

"Hello, Python 101!"

[13]: 'Hello, Python 101!'

You can get Python to tell you the type of an expression by using the built-in type() function.
You’ll notice that Python refers to integers as int, floats as float, and character strings as str.
[14]: # Type of 12

type(12)

[14]: int

[15]: # Type of 2.14

type(2.14)

[15]: float

[16]: # Type of "Hello, Python 101!"

type("Hello, Python 101!")

[16]: str

In the code cell below, use the type() function to check the object type of 12.0.

[17]: # Write your code below. Don't forget to press Shift+Enter to execute the cell

type(12.0)

[17]: float

Click here for the solution


type(12.0)
Integers
Here are some examples of integers. Integers can be negative or positive numbers:
We can verify this is the case by using, you guessed it, the type() function:

6
[18]: # Print the type of -1

type(-1)

[18]: int

[19]: # Print the type of 4

type(4)

[19]: int

[20]: # Print the type of 0

type(0)

[20]: int

Floats
Floats represent real numbers; they are a superset of integer numbers but also include “numbers
with decimals”. There are some limitations when it comes to machines representing real numbers,
but floating point numbers are a good representation in most cases. You can learn more about the
specifics of floats for your runtime environment, by checking the value of sys.float_info. This will
also tell you what’s the largest and smallest number that can be represented with them.
Once again, can test some examples with the type() function:

[21]: # Print the type of 1.0

type(1.0) # Notice that 1 is an int, and 1.0 is a float

[21]: float

[22]: # Print the type of 0.5

type(0.5)

[22]: float

[23]: # Print the type of 0.56

type(0.56)

[23]: float

[24]: # System settings about float type

7
sys.float_info

[24]: sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308,


min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15,
mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

Converting from one object type to a different object type


You can change the type of the object in Python; this is called typecasting. For example, you can
convert an integer into a float (e.g. 2 to 2.0).
Let’s try it:
[25]: # Verify that this is an integer

type(2)

[25]: int

Converting integers to floats


Let’s cast integer 2 to float:
[26]: # Convert 2 to a float

float(2)

[26]: 2.0

[27]: # Convert integer 2 to a float and check its type

type(float(2))

[27]: float

When we convert an integer into a float, we don’t really change the value (i.e., the significand) of
the number. However, if we cast a float into an integer, we could potentially lose some information.
For example, if we cast the float 1.1 to integer we will get 1 and lose the decimal information (i.e.,
0.1):

[28]: # Casting 1.1 to integer will result in loss of information

int(1.1)

[28]: 1

Converting from strings to integers or floats


Sometimes, we can have a string that contains a number within it. If this is the case, we can cast
that string that represents a number into an integer using int():

8
[29]: # Convert a string into an integer

int('1')

[29]: 1

But if you try to do so with a string that is not a perfect match for a number, you’ll get an error.
Try the following:
[30]: # Convert a string into an integer with error

int('1 or 2 people')


,→ ---------------------------------------------------------------------------

ValueError Traceback (most recent call␣


,→ last)

<ipython-input-30-b78145d165c7> in <module>
1 # Convert a string into an integer with error
2
----> 3 int('1 or 2 people')

ValueError: invalid literal for int() with base 10: '1 or 2 people'

You can also convert strings containing floating point numbers into float objects:
[31]: # Convert the string "1.2" into a float

float('1.2')

[31]: 1.2

[Tip:] Note that strings can be represented with single quotes (<code>'1.2'</code>) or double q
Converting numbers to strings
If we can convert strings to numbers, it is only natural to assume that we can convert numbers to
strings, right?
[32]: # Convert an integer to a string

str(1)

[32]: '1'

9
And there is no reason why we shouldn’t be able to make floats into strings as well:
[33]: # Convert a float to a string

str(1.2)

[33]: '1.2'

Boolean data type


Boolean is another important type in Python. An object of type Boolean can take on one of two
values: True or False:
[34]: # Value true

True

[34]: True

Notice that the value True has an uppercase “T”. The same is true for False (i.e. you must use the
uppercase “F”).

[35]: # Value false

False

[35]: False

When you ask Python to display the type of a boolean object it will show bool which stands for
boolean:
[36]: # Type of True

type(True)

[36]: bool

[ ]: # Type of False

type(False)

We can cast boolean objects to other data types. If we cast a boolean with a value of True to an
integer or float we will get a one. If we cast a boolean with a value of False to an integer or float
we will get a zero. Similarly, if we cast a 1 to a Boolean, you get a True. And if we cast a 0 to a
Boolean we will get a False. Let’s give it a try:
[37]: # Convert True to int

int(True)

10
[37]: 1

[38]: # Convert 1 to boolean

bool(1)

[38]: True

[39]: # Convert 0 to boolean

bool(0)

[39]: False

[40]: # Convert True to float

float(True)

[40]: 1.0

Exercise: Types
What is the data type of the result of: 6 / 2?

[41]: # Write your code below. Don't forget to press Shift+Enter to execute the cell
type(6/2)

[41]: float

Click here for the solution


type(6/2) # float
What is the type of the result of: 6 // 2? (Note the double slash //.)

[42]: # Write your code below. Don't forget to press Shift+Enter to execute the cell
type(6//2) #integar

[42]: int

Click here for the solution


type(6//2) # int, as the double slashes stand for integer division
Expression and Variables
Expressions
Expressions in Python can include operations among compatible types (e.g., integers and floats).
For example, basic arithmetic operations like adding multiple numbers:

11
[43]: # Addition operation expression

43 + 60 + 16 + 41

[43]: 160

We can perform subtraction operations using the minus operator. In this case the result is a
negative number:
[44]: # Subtraction operation expression

50 - 60

[44]: -10

We can do multiplication using an asterisk:


[45]: # Multiplication operation expression

5 * 5

[45]: 25

We can also perform division with the forward slash:


[46]: # Division operation expression

25 / 5

[46]: 5.0

[47]: # Division operation expression

25 / 6

[47]: 4.166666666666667

As seen in the quiz above, we can use the double slash for integer division, where the result is
rounded down to the nearest integer:
[48]: # Integer division operation expression

25 // 5

[48]: 5

[49]: # Integer division operation expression

25 // 6

12
[49]: 4

Exercise: Expression
Let’s write an expression that calculates how many hours there are in 160 minutes:
[56]: # Write your code below. Don't forget to press Shift+Enter to execute the cell

Minutes = 160
Hr = (Minutes/60)
print(Hr)

2.6666666666666665
Click here for the solution
160/60

# Or

160//60
Python follows well accepted mathematical conventions when evaluating mathematical expressions.
In the following example, Python adds 30 to the result of the multiplication (i.e., 120).

[57]: # Mathematical expression

30 + 2 * 60

[57]: 150

And just like mathematics, expressions enclosed in parentheses have priority. So the following
multiplies 32 by 60.
[58]: # Mathematical expression

(30 + 2) * 60

[58]: 1920

Variables
Just like with most programming languages, we can store values in variables, so we can use them
later on. For example:
[59]: # Store value into variable

x = 43 + 60 + 16 + 41

To see the value of x in a Notebook, we can simply place it on the last line of a cell:

13
[60]: # Print out the value in variable

[60]: 160

We can also perform operations on x and save the result to a new variable:
[61]: # Use another variable to store the result of the operation between variable␣
,→and value

y = x / 60
y

[61]: 2.6666666666666665

If we save a value to an existing variable, the new value will overwrite the previous value:
[62]: # Overwrite variable with new value

x = x / 60
x

[62]: 2.6666666666666665

It’s a good practice to use meaningful variable names, so you and others can read the code and
understand it more easily:
[63]: # Name the variables meaningfully

total_min = 43 + 42 + 57 # Total length of albums in minutes


total_min

[63]: 142

[64]: # Name the variables meaningfully

total_hours = total_min / 60 # Total length of albums in hours


total_hours

[64]: 2.3666666666666667

In the cells above we added the length of three albums in minutes and stored it in total_min. We
then divided it by 60 to calculate total length total_hours in hours. You can also do it all at once
in a single expression, as long as you use parenthesis to add the albums length before you divide,
as shown below.

14
[65]: # Complicate expression

total_hours = (43 + 42 + 57) / 60 # Total hours in a single expression


total_hours

[65]: 2.3666666666666667

If you’d rather have total hours as an integer, you can of course replace the floating point division
with integer division (i.e., //).
Exercise: Expression and Variables in Python
What is the value of x where x = 3 + 2 * 2
[66]: # Write your code below. Don't forget to press Shift+Enter to execute the cell
x = 3+2*2
x

[66]: 7

Click here for the solution


7
What is the value of y where y = (3 + 2) * 2?

[68]: # Write your code below. Don't forget to press Shift+Enter to execute the cell
y = (3+2) *2
y

[68]: 10

Click here for the solution


10
What is the value of z where z = x + y?
[71]: # Write your code below. Don't forget to press Shift+Enter to execute the cell
z=x+y
z

[71]: 17

Click here for the solution


17
The last exercise!
Congratulations, you have completed your first lesson and hands-on lab in Python. However, there
is one more thing you need to do. The Data Science community encourages sharing work. The
best way to share and showcase your work is to share it on GitHub. By sharing your notebook

15
on GitHub you are not only building your reputation with fellow data scientists, but you can also
show it off when applying for a job. Even though this was your first piece of work, it is never too
early to start building good habits. So, please read and follow this article to learn how to share
your work.

1.2 Author

Joseph Santarcangelo

1.3 Other contributors

Mavis Zhou

1.4 Change Log

Date (YYYY-MM-DD) Version Changed By Change Description


2020-08-26 2.0 Lavanya Moved lab to course repo in GitLab

##
© IBM Corporation 2020. All rights reserved.

16

You might also like