Skip to content
Christopher Vardakis edited this page Nov 23, 2022 · 1 revision

Python

Python is a programming Language. To make a script (aka program) in python there are two ways:

  • file
> nano script.py
/* edit *
> python script.py
Result
  • interpreter
> python
>>>> //write commands here and enter to run

Comments

# this line doesnt run

printing

To print something to the screen

print("Hello World")
Hello World

strings

Strings are basically text (aka a series of characters). To convert something like a number to a string you can use 2 methods:

  • convert to a string and add to a string example:
	i = 2 # this is a number
	my_str = "Two = " + str(i)
	print(my_str)
Two = 2
  • Use String formatting (f before string and variable or expresion in brackets) example:
i = 2
	my_str = f"Two = {i}"
	print(my_str)
Two = 2

for loop

do something N times (giving a variable values from I to N)

for variable in range(from, to, step):
# "step" defaults to 1 and "from" to 0. 
# to is not included
# needs tab to put code inside a loop
	do this

#example
for i in range(10):
	print(i)
print("Once")
0
1
2
3
4
5
6
7
8
9
Once
Clone this wiki locally