print("Hello There!") # Welcome the user to the Basics page
Hello There!On this page you will find some basics of python such as printing, lists, variables, etc.
print("Hello There!") # Welcome the user to the Basics page
Hello There!On this page you will find some basics of python such as printing, lists, variables, etc.
Printing is how you can display text for your user to read
# This is how you print print("Hello World!")The output for this is:
Hello World!
Comments are used to inform whoever is reading your code about what your code does. A comment is created by using a # followed by a space and a capital letter (unless the first word is an identifier). Comments don't appear in your outputted code, they are there for humans to read not computers
# This is a comment
At the start of any python file you create you should include a few comments about your program e.g.
# name_of_file.py # Description of program # Date created # Your name/user name
When commenting there are 2 ways of doing it, in-line commenting or block commenting. A block comment usually applies to the block of code beneath it and should be indented accordingly. An in-comment is used to describe what a certain line of code is doing
# This is a block comment print("Hello World!") # This is an in-line comment
When commenting your comments should be D.R.Y. or "Don't Repeat Yourself" In other words don't write out exactly what your code does. Here is an example of a W.E.T. (Wrote everything twice) comment
print("Hello There!") # Prints Hello There!
A variable is
var = "This is how you define a variable"
You can then use the variable in other parts of your code e.g. printing
print(var)
This is how you define a variable
You can use an input to get user input into your program
input("This is an input ")
This is an input
You can also use an input to set a variable
answer = input("What does 9 + 10 = ")
What does 9 + 10 =
print(answer)
19
if answer == 19: print("Correct!") else: print("Incorrect!")
Correct!
items = ["Item 1", "Item 2", "Item 3"] print(items)
['Item 1', 'Item 2', 'Item 3']
In python there are 2 loops, a while loop and a for loop. Here are some examples
# Demonstrate a for loop for i in range(5): print(i)
0 1 2 3 4
# Demonstrate a while loop i = 0 while i < 5: print(i) i += 1
0 1 2 3 4