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.

printhelloworld.png

result from running code in Python Shell

Strings

String- a sequence of characters.

strings.png

examples of strings in Python

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

concatenating.png

concatenate two or more strings using the + operator

helloandworld.png

Concatenating “Hello” and “World”

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.

hellospaceworld.png

Notice how the space is in a different place in each line, but the resulting string is the same.

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

deffunction.png
callhelloworld.jpg

After running the code for the function definition, we are ready to call our function.

We just use the function name and the parentheses. Since our parentheses were empty in the function definition, we leave them empty here.

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.

hellox.jpg

I’ve put a parameter, x, in the parentheses. It represents the name that we’ll pass to this function when we call it. In the function body, I used concatenation to create a new string that is hello, a space, then the parameter x.

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.

hellosamantha.png

We call the function using the function name, and this function is expecting an argument as a string, so I’ve used my name in quotation marks.

We can call this function again, and again with different names, and even…

helloeveryone.jpg

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.

Next
Next

02 - Data Types, Variables, If-Else, Comments