Subscribe to my email list now at http://jauyeung.net/subscribe/
Follow me on Twitter at https://twitter.com/AuMayeung
Many more articles at https://medium.com/@hohanga
Even more articles at http://thewebdev.info/
Python is a convenient language that’s often used for scripting, data science, and web development.
In this article, we’ll look at how to create our first Python program.
Our First Program
We can create a program that asks us our name and then displays a greeting with our name as follows:
print('What\'s your name?')
name=input()
print('Hello', name)
In the code above, we called the print
function to show the What's your name?
message. we have a \
before the '
to indicate that we aren’t closing the quotes with the single quote.
Then we call the input
function to get the value that’s inputted with the keyboard.
Once we press Enter, then the name
variable will be assigned to what we’ve entered.
Then we display Hello
and then whatever we entered before.
For example, if we entered Joe
then we get Hello Joe
displayed on the screen.
If we want to add an input for age and display it, we can rewrite the program to be the following:
print('What\'s your name?')
name=input()
print('What\'s your age?')
age=input()
print('Hello', name, 'You are', str(int(float(age))), 'years old')
print('The length of your name is', str(len(name)))
In the code above, we added an age
input with:
age=input()
Then we converted the inputtedage
to a float with float
, then we converted it to an integer with int
, then we convert it back to string with the str
function.
We did that to round any decimals to a whole number.
Then we use print
to display everything in English.
Then we get something like:
What's your name?
Joe
What's your age?
20.2
Hello Joe You are 20 years old
Where Joe
and 20.2
are what we entered.
Parts of our Program
A program consists of multiple parts.
Comments are one part of a program that serves as documentation that explains what we want to do with the program.
Python ignores comments, which starts with #
.
It’s also used to temporarily remove a line before running a program for debugging purposes.
Python also ignores blank lines after the comment, we can add more blank lines to make our program easier to read.
The print() Function
The print
function lets us print strings on the screen. We can separate the parts of our string with parentheses.
We did that above with:
print('What\'s your name?')
and:
print('What\'s your age?')
and:
print('Hello', name, 'You are', str(int(float(age))), 'years old')
Note that everything we passed into the print
function must be strings. The end quotes are delimiters for our string.
We can print a blank line by passing nothing to the print
function.
The input() Function
The input
function lets us take in input from users by letting the typing it in on the screen.
It returns a string with the things that are typed.
We get the name with name=input()
, and then passed it into the print
function to print it out.
The len() Function
The len
function lets us get the length of a string by passing in a string.
In the example above, we have:
print('The length of your name is', str(len(name)))
to get the length of the name
string and print it out.
So for 'Joe'
, it’ll be 3.
The str(), int(), and float() Functions
The str
function was used to convert any non-string expression to a string.
Likewise, int
and float
are used to convert something an integer and floating-point number respectively.
For example, we can convert a float to an integer by writing:
int(1.11)
then we get 1.
If we try to convert something that can’t be converted to an integer as follows:
int('11.99')
We’ll get the following error:
ValueError: invalid literal for int() with base 10: '99.99'
Likewise, we can convert something to a floating-point number as follows:
float('11.99')
Then we get 11.99
returned.
We can pass in any other expressions into functions not just literals like strings and numbers.
Conclusion
We can print strings out to the screen with the print
function. It takes any number of strings separated by parentheses.
To get input from a user, we can use the input
function, which returns a string with whatever is entered by the user after they press enter.
To get the length of a string, we can use the len
function, which takes a string that we want to get the length for as an argument.
To convert something to a string, we use the str
function. Likewise, we convert to a floating-point number with the float
function, and an integer with the int
function.
In addition to code, we can put comments in our Python program, which are ignored by the Python interpreter. They start with a #
sign.