Unit 1: Introduction to Python 3
This unit will introduce you to the Python 3 programming language and cover how to use a platform-independent web-based programming environment to begin writing basic Python scripts. We will introduce basic Python data types, the assignment operator, and how to output data to the screen.
Completing this unit should take you approximately 3 hours.
Upon successful completion of this unit, you will be able to:
- use an integrated development environment (IDE) to write simple programs;
- explain the int and float data types;
- explain variable assignment;
- apply basic Python output using the print instruction; and
- explain the string data type.
1.1: Introduction to Python 3
Python is an ideal language for learning to program. Watch this video to see just how useful Python can be. After this, we will introduce an integrated development environment (IDE) and begin programming. The IDE will make it simple to quickly visualize and experience the results of a set of Python programming instructions.
1.2: Accessing the Repl.it IDE
In this course, we will use the Repl.it IDE. This web-based IDE requires no installation, which means that you can begin programming in Python immediately. We will use this IDE throughout the course. On the page, you should see three windows arranged side-by-side in columns. In this unit, we will focus on the rightmost window. This window has a command line prompt, which is indicated by the
>
. We can type basic Python commands using this command prompt. As we progress through the course, we will start to use the middle and left windows.
1.3: Python 3 Data Types: int and float
- Programming languages must distinguish between different types of data. Since we want to introduce simple Python computations, it makes sense to discuss numerical data. The goal of this section is to introduce two fundamental numerical data types: int and float. The int data type refers to integer data. Numbers not containing digits to the right of the decimal point such as
- 10
,0
, and357
are integers. The float data type refers to floating-point data. Numbers containing digits to the right of the decimal point such as1.3
,-3.14
, and300.345567
are floating-point numbers.After you complete this section, you should be able to explain the difference between int and float. There are deeper reasons (beyond the scope of this course) why you might distinguish between these data types; those have to do with how they are represented within a computer system. Our goal for this section is simply to be able to visually identify integer and floating-point numbers. On this page, you will see more examples of int and float.
The int data type refers to integer numerical data. In the rightmost window of the Repl.it IDE, use your mouse to click to the right of the > command prompt. Next, type the integer
2
and press the "enter" key on your keyboard. You should see the IDE echo the value back to the screen. Try this a few times with other examples to be sure you understand the int data type.The float data type refers to floating-point numerical data. In the rightmost window of the Repl.it IDE, use your mouse to click to the right of the > command prompt. Next, type the floating-point number
2.57
and press the "enter" key on your keyboard. You should see the IDE echo the value back to the screen. Try this a few times with other examples that have been provided to be sure you understand the float data type.It is of the utmost importance that you understand the value
2
is of type int, while2.0
is of type float. On paper, this might not seem like a big difference, but the decimal point is how a computer tells the difference between these two data types. Another way of typing floating-point data is to use scientific notation. For instance, the number 0.0012 can also be entered as 1.2e-3. In this example, "e-3" means "10 to raised to the minus three power" and this value is multiplied by 1.2; hence, 1.2e-3 is equivalent to 0.0012. Try typing
0.0012
into the Repl.it command line, then try entering1.2e-3
. You should see that Python views both entries as the same value. Scientific notation is very useful when the exponents are very positive or very negative. In data science, exponents in numbers such as -5.63e127 or 2.134589e-63 would not be uncommon. Rather than writing out numbers of this kind in their full form, scientific notation offers a more compact, readable form for presenting float data. If you need to review scientific notation, watch this video.
1.4: Variable Assignment
Computers must be able to remember and store data. This can be accomplished by creating a variable to house a given value. The assignment operator = is used to associate a variable name with a given value. For example, type the command:
a=3.45
in the command line window. This command assigns the value 3.45 to the variable named
a
. Next, type the command:a
in the command window and hit the enter key. You should see the value contained in the variable
a
echoed to the screen. This variable will remember the value 3.45 until it is assigned a different value. To see this, type these two commands:a=7.32
and then
a
You should see the new value contained in the variable
a
echoed to the screen. The new value has "overwritten" the old value. We must be careful since once an old value has been overwritten, it is no longer remembered. The new value is now what is being remembered.Although we will not discuss arithmetic operations in detail until the next unit, you can at least be equipped with the syntax for basic operations: + (addition), - (subtraction), * (multiplication), / (division)
For example, entering these command sequentially into the command line window:
a=7.32 b=a+5 b
would result in 12.32 being echoed to the screen (just as you would expect from a calculator). The syntax for multiplication works similarly. For example:
a=7 b=a*5 b
would result in 35 being echoed to the screen because the variable
b
has been assigned the valuea*5
where, at the time of execution, the variablea
contains a value of 7.After you read, you should be able to execute simple assignment commands using integer and float values in the command window of the Repl.it IDE. Try typing some more of the examples from this web page to convince yourself that a variable has been assigned a specific value.
Python has its own set of reserved words that, in general, should not be chosen as variable names. As we dive deeper into the course, it will become clearer how to apply these reserved words. For now, just be aware that your variable name choices should avoid the words on this list. Otherwise, though, you can choose any variable name you like. It is important to think about how variable names should be chosen in practical applications. To help others understand your work, you should choose variable names that fit their applications. For example:
account_balance=2034.12
might reflect the balance contained in a bank account. You will build the skill of sensibly choosing variable names naturally as you work through more programming examples.
1.5: Basic Python Output Using the print Function
The "print" function is a Python instruction that will output variable values and results to the computer screen. This instruction will allow us to automatically output data results to the screen when running a program. This section reviews what we've covered so far and introduces examples of the print function. Try executing these instructions in the command line window to make sure you understand how to use the print function:
z=1.45 print(z)
Notice how the print function outputs the value of the variable
z
to the screen.It is possible to use the print command in a slightly more sophisticated way. Type this set of commands in the command window:
temperature=40 print('Temperature outside = ', temperature)
Observe, it is possible to "dress up" the screen output by adding some extra descriptive text. We will see more examples of this later on as we introduce and develop expertise with the string data type. But, as a preview of using the print function with text, try typing this command:
print('Hello world!')
Congratulations! You have now executed what is probably the most-used example in just about every introductory programming course.
Read this page to see more examples of using the print function. Try typing some of those examples in the Repl.it IDE to be sure you are comfortable with the print function. Consider executing these instructions one after the other (that is, sequentially):
var1=22 var1=-35 var1=308
What value will the variable
var1
contain after these instructions are executed? You can check your answer by using the print function.
1.6: More Python 3 Data Types: str
Our introduction would not be complete without discussing how to work with text and character data. In this section, you will learn about the string data type and assigning a variable of type "str". String data is text or character data. To assign a variable of type str, the text string must be placed in quotes:
string_example='This is the text string'
To see that this variable assignment has taken place, type the command:
print(string_example)
and you should see the text string echoed to the screen. To assign a variable of type str, you can use either single or double quotes, but single quotes are often the convention of choice. As we dive deeper into the course, many interesting operations and computations using the string data type will be presented. Read this page to learn more about the string variable type. Use the Repl.it IDE to practice some of the string commands.
The
type
FunctionThe
type
function is an instruction that can tell us the type of a variable after it has been assigned a value. We can practice using this instruction and the print function as we study data types in this section. Try typing these instructions in the command line window:a=5 print(a) print(type(a))
Try typing this set of commands to see how Python distinguishes between the three data types introduced so far. Notice how the type command is used with the print instruction to tell us the data type after a given variable has been assigned.
a=3.4 print('a=',a) print(type(a)) b=3 print('b=',b) print(type(b)) c='name' print('c=',c) print(type(c))
In the Repl.it script/run window, as the number of instructions begins to increase, it can be difficult to type them one by one in the command line. The remedy for this issue is to run several commands at once in what is known as a Python "script". The middle window in Repl.it makes this possible. Try copying the above set of commands and pasting them into the middle window beginning at line 1. If you have done this correctly, you should see words that the Python language recognizes (such as print and true) in blue. Now press the "run" button at the top of the page. You should see the expected screen output appear in the rightmost command window.
Predict the output of the type function for each of these variable assignments:
var1 = 3.214e-12
var2 = 'Wednesday'
var4 = -32
You can check your answers using the print function with the type function as discussed above.
Consider executing these commands in the script window (the middle window in Repl.it starting at line 1):
a=3 print('a=',a) print(type(a)) b=3.0 print('b=',b) print(type(b))
Before pressing the "run" button, predict the output to the screen. In other words, mentally execute this code before running it to make sure you understand the data type concepts presented in this unit.
Now, try executing these commands in the script window (the middle window in Repl.it starting at line 1):
a=-3.527e3 print('a=',a) print(type(a)) b=-3257.0 print('b=',b) print(type(b))
Again, before you press the "run" button, predict the output to the screen by mentally executing this code to make sure you understand the data type concepts presented in this unit.
a=3 print(a) print(type(a)) b=str(a) print(b) print(type(b))
In this example, the variable
a
has been cast as a string using the "str" command. Sometimes it may be necessary to change the type of a variable. Pay close attention to how the variable type changes in this example:a='75' print(a) print(type(a)) b=int(a) print(b) print(type(b)) c=float(b) print(c) print(type(c))
Read these examples of using the 'print' and 'type' functions. You can also try to use them in the Repl.it command line.
Unit 1 Assessment
- Receive a grade
Take this assessment to see how well you understood this unit.
- This assessment does not count towards your grade. It is just for practice!
- You will see the correct answers when you submit your answers. Use this to help you study for the final exam!
- You can take this assessment as many times as you want, whenever you want.