Skip to content

Latest commit

 

History

History
79 lines (64 loc) · 1.71 KB

File metadata and controls

79 lines (64 loc) · 1.71 KB

Back to index


Contents


Hello World

Typically the first program that will be created

the statement below calls the print() function, which will output the value hello world to the screen.

# Ye Olde Hello World
print("Hello World")

Will output:

Hello World  

Code

Print Formatting

Printing on multiple lines:

# Ye Olde Hello World
print ("Hello\nWorld")

Will output:

Hello 
World

F-strings

Variables can be included in print statements as follows:

# Define variable
num = 3

# Print with a variable
print ("Hello World", num)

(See Types for more information about variables and strings. )

This can be done more elegantly with f-strings which can interpolate variables, objects, and expressions directly into strings. It also allows better control over spacing. By prefixing a string with f or F, expressions can be embedded within curly braces ({}), which are evaluated at runtime.

# Variables
name = "Bob"
age = 30

# Print with f-strings
print(f"Hello, my name is {name} and I'm {age} years old.")

Will output:

Hello, my name is Bob and I'm 30 years old.

Code Comments

Comments improve the readability of code, and should be used to explain what code does.
Comments are ignored at runtime, so they can also be helpful for disabling parts of code where necessary.

# This is a single-line comment
"""
This is a 
multi-line comment
"""