Hello World - CTXz/Python-Sandbox GitHub Wiki


Subject : HelloWorld.py


Like in every other programming language, a simple Hello World output is a great way to start as it gives a rough overview about the simplicity and syntax of a programming language.

In python, writing a Hello World program takes no more than a single function call.

Comments

Before we get to the print() function, Its worth noting about the lines stating with #

Lines starting with a # are comments and get ignored by the interpreter. Comments give the reader a brief overview about a specific part of code. Their job is to save the readers time, and avoid unnecessary line-by-line stepping.

# This is a comment and will be ignored
This is not a comments and will result in an error

Priting

Most beginners may think of a printer when you use the term print

In most programming languages, the function to output text to your terminal is called print. Where a printer prints text (or even images) to paper, the print function prints its text to the tty terminal.

The print function works just like any other python function. It is first called by its function name, being print following parentheses where the function parameters/arguments are defined. In this case the function parameter(s) define the printed output. In our example program, we're passing "Hello World" as a parameter to print.

Types

You may have noticed that we're using quotation marks around the Hello World parameter. Most programming language come with data types. Data types indicate what kind of data is being read by the computer. Whether it is a text, number or condition. The interpreter needs some kind of sign how to differentiate code, text and numbers. Python does a great job at doing the most type work for you, but don't be fooled, it is crucial to know about data types as you'll often find yourself in a situation where you must convert from one type to another.

To get the type of a value, use the type(x) function, where x is the value parameter. Because the type(x) function returns the type itself, it will print <class 'type'>. To get the name of the type as a string, you can call the name member of the type object. See example

Example:

print(type(20))                          # This will print <class 'int'>
print(type(20).__name__)                 # This will print int

print(type("this is a string").__name__) # This will print str

Types become particularity useful when working with variables and function parameters, which will both be discussed later

str

Short for string, this data type indicates plain text. A string is marked by wrapping the text either around double quotes, or single quotes.

"This is a string"
'This too'
This not

int

Short for integer, this data type indicates a non-decimal value and allows arrhythmic operations such as addition, subtraction division and multiplication.

10   # This is an int
1000 # This is an int too
'10' # This is not a int but a string

# String vs int
'10' + 10 # This will not work
10 + 10   # This will work and result in 20

float

This data type indicates a decimal value arrhythmic operations such as addition, subtraction division and multiplication with decimal support.

1.45  # This is a float
0.123 # This is a float
1     # This is a int

1.2 + 1.3   # This will result in 1.5
1   + 1.5   # This will result in 2.5 and return a float as result
'1.5' + 1.2 # This will not work

bool

This data type indicates a conditional that can either be True or False, where True may also be written as 1 or False may also be written as 0. Booleans are mainly used in conditional statements which will be discussed later.

True  # This is a true boolean
False # This is a false boolean
1     # This may be used to indicate a true boolean, but will be assigned as int by default
0     # This may be used to indicate a false boolean, but will be assigned as int by default

bool(1) # This will return true
bool(0) # This will return false

Converting types

Converting types can be done by calling type functions. Those are called by indicating the type, followed by parenthesis, where the value who's type is to be converted, is passed as parameter. The function then returns the passed value as the new type.

Example:

"I'm " + str(21) + " years old" # This will work as we convert an int to a string
"I'm " + "21" + " years old"    # This will work too

10 + int("10")                  # This will work and result in 20
10 + 10                         # This will work too

As you can see, the converting isn't very useful in our example since we're not working with variables yet.