Python 01 - Hello World!, Strings and Functions Basics
Hello World!
A Hello World! is traditionally the first program one might create in a new language. What this program does is it displays the words “Hello World!”
Example One
In Python, this can be done in one line.
Strings
String- a sequence of characters.
Strings can go between ‘single quotes’ or “double quotes” and even ‘‘‘triple single quotes’’’ or “““triple double quotes”””
Concatenating Strings
Concatenate (verb) - to link together in a series or chain. (Merriam-Webster)
using the + operator
Strings can be concatenated or joined to form a new, longer string.
Strings are placed right next to each other
Every part must be a string (i.e. in quotation marks), otherwise you’ll get an error
It’s important to remember that when you concatenate strings, you’re literally putting them right next to each other, so you may want to include a space.
Functions
Let’s say you wanted to do Hello World! multiple times. You might just type print(“Hello World!”) every time since it is only one line.
Alternatively, you could create a function that does Hello World! and simply call the function each time.
This example might seem trivial, but say you had a hundred lines of code that did something, and you wanted to repeat that something multiple times. It would be a lot to type or copy and paste, and your code would end up looking messy. In this case you would definitely want to create a function because, yes, it would be less typing, but your code would also be more readable and easier to understand because of the way you’ve broken down your program.
Example Two
A function is a named block of code.
Typically a (good) function will do something, a complete action.
Creating a function that does Hello World!
First, we have to define the function. We have to give the function a name and write the code for what the function will do.
The function will be named hello_world
The code in the function body will be the same code from the original Hello World program (Example One)
Defining a Function
We can call this function again, and again.
Let’s create a function that says hello to different people, like “Hello Samantha” or “Hello Someone”
We want to make it so that when we call this function, we are able to pass it a name, and it can take that name and print Hello and that name.
I’ll call this function hello.
Function Parameter- a variable used in the definition of a function.
Recall: In order to use the plus operator to concatenate strings, all parts must be a string. This means that the parameter x must represent a string. So when we call this function, we must pass it a name as a string (in quotation marks), otherwise we’ll get an error.
It’s important to note that the parameter x is not a string, but it is a variable which represents a string.
After running the previous code we can call our function.
Function Argument- value(s) that must be passed to the function. An argument replaces the parameter in the function definition.
We can call this function again, and again with different names, and even…
So you might be thinking,
print(“Hello World!”)
looks like a function call… And it is!
You can imagine that somewhere in the Python Libraries there’s a definition for a function called print and when we call that function, we’re passing it a string and the function displays that string.