Your first Python program
Baha Abu-Shaqra, PhD (DTI uOttawa)
Network Engineering (career change)
In this post we will create our first Python program. We're using Mu Editor (but you can use IDLE or any other Python editor). This post introduces the concepts of execution, functions (print, input, length, string, integer), and arguments. This post is part two of an eight-part series covering necessary background for the Cisco DevNet and ENAUTO streams certifications.
You may also be interested in Pre-DevNet (Python, Linux, Bash).
Before we dive in, I try to make the world a little better. You're invited to read my letter to uOttawa President?Jacques Frémont about how to easily implement policy reforms to prevent supervisor bullying of uOttawa students: uOttawa President Jacques Frémont ignores university bullying problem. You may also be interested in How to end supervisor bullying at uOttawa.
Introduction
Launch Mu Editor (or IDLE), click New to open a new script file. Enter the following code and then save the file as Our_first_program.py.
print('Hello, world!')
print('What is your name?') # ask for their name
myName = input()
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?') # ask for their age
myAge = input()
print('You will be ' + str(int(myAge) + 1) + ' in a year.')
print('Say, ' + myName + ', ' + 'do you like chicken?')
user_input = input()
if user_input.lower() == 'yes':
print('Bock bock bock bawk!')
else:
print('I see, I see.')
print('What do you like to do, ' + myName + '?')
user_input = input()
print('Good to know.')
After saving the file click Run to execute the code.
You will be greeted with the following two lines - and you can start interacting with the program by typing your name and pressing enter.
Hello, World!
What is your name?
In the above example, the user entered their name as Goofy.
Execution
Python starts at the first instruction at the top of the program and then moves downward, executing each instruction entered. The execution in Python refers to the instruction being currently executed.
The line with the # sign is called a comment. Python ignores comments. Comments serve as (explanatory) notes about what the code is trying to do or what the entire program does. Python ignores blank lines.
The print() function
Functions in Python are like mini-programs. Functions contain code that does various things. Python comes with a lot of functions.
In code, a function is its name followed by parenthesis, and optionally sometimes there are values passed to the function inside the parentheses.
The print() function displays the string value inside the parenthesis on the screen. So the following line means print out the text in the string 'Hello World':
print('Hello World')
Arguments
When Python executes the line print('Hello, world!'), we say that Python is calling the print() function and the string value is being passed to the function. A value that is passed to a function call is an argument. But really values and arguments are the exact same thing.
This is called "calling the print function":
print('Hello World')
The following instruction is also a call to the print function. This time it's passing the 'What is your name?' string to the print function.
print('What is your name?')
Input function
When the input function is called, Python waits for the user to type some text on the keyboard and press enter.
myName = input()
Like a variable evaluates to the value it contains, calls to the input function evaluate to the string value of what the user typed.
For example, if we typed in Goofy when input() was called, myName variable assignment statement will evaluate to Goofy, and then the string will get stored inside myName.
myName = input()
myName = 'Goofy'
Remember, expressions evaluate down to a single value. So if Goofy is stored in myName, then this is what that call to the print function looks like.
print('It is good to meet you, ' + myName)
print('It is good to meet you, ' + 'Goofy')
print('It is good to meet you, Goofy')
Here, 'It is good to meet you, Goofy' is the string that gets passed to the print() function call.
Length function
The following line introduces the len function.
print(len(myName))
len takes a string argument and then evaluates to the integer value of the length of the string, which is the number of characters in the string. Examples:
len('Albert')
领英推荐
6
len('')
0
We can use these function calls to len anywhere we could use an integer.
len('Goofy') * 10
50
String and integer functions
Line 9 in Our First Python Program diagram (line 10 in the featured image) introduces the string and integer functions:
print('You will be ' + str(int(myAge) + 1) + ' in a year.')
The str() function lets us concatenate an integer or a float to a string. The int() function lets us do math with numbers as string values.
The str() and int() functions return string and integer values of whatever you pass them. This is handy if you need to convert between data types.
The input() function always returns a string value, even if the user enters a number.
Enter spam = input() into the interactive shell and enter 101 when it waits for your text.
spam = input()
101
spam
'101'
spam + 1
'101' + 1
At this point you will get an error. TypeError: can only concatenate str (not "int") to str
But,
int('101') + 1
102
To do math on a string, we first have to get an integer value from the input function. Likewise, we cannot do string concatenation on integers.
27 + ' years old'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
But,
str(27) + ' years old'
'27 years old'
Now we can make sense of the entire line in our program, how Python evaluates it.
print( 'You will be ' + str(int(myAge) + 1) + ' in a year.')
print( 'You will be ' + str(int('26') + 1) + ' in a year.')
print( 'You will be ' + str(26 + 1) + ' in a year.')
print('You will be ' + str(27) + ' in a year.')
print('You will be ' + '27' + ' in a year.')
print('You will be 27' + ' in a year.')
print('You will be 27 in a year')
*Featured image was created with Mu.
Key references
Automate the Boring Stuff with Python – Al Sweigart free online book (free to read under a CC license)
Automate the Boring Stuff with Python – Al Sweigart YouTube playlist
Commercialisation and CFO Advisory Services - "I crunch numbers to make peoples' lives better"
1 个月Well done cuz! I took the foundation course 3 years ago using Anaconda as a platform. Wait until you get to the libraries; there is the real power of Python. All the best.