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.
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.
In this course, we will use the Repl.it IDE. This web-based IDE requires no installation so that you can begin programming in Python immediately. We will use this IDE throughout the course.
To start a Python Repl and run single Python commands:
>>>
will be displayed.You must have JavaScript enabled in your browser to use the IDE.
-
10
, 0
, and 357
are integers. The float data type refers to floating-point data. Numbers
containing digits to the right of the decimal point such as 1.3
, -3.14
,
and 300.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, while 2.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 entering 1.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.
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 value a*5
where, at the time of execution, the variable a
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.
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.
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.
Read these examples of using the 'print' and 'type' functions. You can also try to use them in the Repl.it command line.
In this video, course designer Eric Sakk walks through the major topics we covered in Unit 1. As you watch, work through the exercises to try them out yourself.
Take this assessment to see how well you understood this unit.