If you are able to understand how to use pound symbols (#) when writing your code, it'll save you a lot of time. When this is put in front of something, the rest of the line will be ignored. So, if this is put at the beginning of a line, the entire line will be ignored.
You can use the pound sign after your code in order to comment on it. Comments allow you to make notes about your code, what it's doing, and if there is something that needs to be fixed. They are very helpful to understand your own code and to help others do the same.
You can also employ pound signs at the beginning of lines –– in front of your written code. By doing this, you can exclude various statements in order to figure out if the skeleton outline of your code is correct. For example, if you are writing a code to drive a cart that uses a while loop, you can use some pound symbols in front of your ServoWrite statements. This way, you can check to see if your while loop is working correctly without deleting your work to check.
Useful code commands in Python:
len() → number of characters
lower() → no capitalization
upper() → all capitalization
list.append(item) → adds to a list
.sort() → organizes a list
.remove() → removes something from a list
quit() → quits python
Python3.7 name of file → runs a file in terminal
execfile(“name of file”) → runs a file in python
grep ‘word’ file name → searches for a word in a file
yield → returns a generator (keeps values as it goes)
EDITING AND DELETING FASTER:
Clean up the line: use CRtl
Even though it may not seem important, indentation in Python is crucial. Code will not run properly unless it is indented properly. So, here are some times when you need to indent:
underneath a while statement (Ex. while x == 1:)
underneath a for statement (Ex. for x in range(1))
underneath an if statement (Ex. if x == 1)
underneath an else statement (Ex. else:)
underneath an elif statement (Ex. elif x == 2)
Essentially, you need an indentation in the line following a statement that gives conditions. Also, if there is a piece of information within a loop, that must be indented as well. Here's an example:
for a in range(1,n):
for b in range(a,n):
c_square = a**2 + b**2
c = int(sqrt(c_square))
if ((c_square - c**2) == 0):
print(a, b, c)
As shown in this example, although "c_square = a**2 + b**2" is not a direction (it's only a statement), it still needs to be indented because it is within a for statement. Also, like in the example, sometimes if statements are embedded into for statements. In this case, it still needs to have the proper indent, and the direction following the if statement must be indented twice.
If you are new to python, here is the code to a tutorial on the basics of python.
Copy the following code, make sure you are in python in your terminal, then paste it there.
from time import sleep
def tutorial():
print("Welcome to your tutorial!")
sleep(3)
print("This tutorial will teach you the very basics of python")
sleep(5)
x = raw_input("To start off, we will be playing a very short game. Please press enter to begin. ")
def game():
print("Hello! And welcome to Guessinator")
sleep(2)
maybe = raw_input("To continue, press enter ")
print("Alright, let's start!")
sleep(2)
print("Please choose something from the following list: \n A whale \n A baseball \n An airplane \n A rocket ship \n A boat \n A bicycle \n A dog")
sleep(9)
print("please answer the following questions with 'yes' or 'no'")
sleep(3)
w = raw_input("Is this thing alive? ").lower()
if w == "yes":
w = raw_input("Does this thing live in the water? ").lower()
if w == "yes":
print("It's a whale!")
else:
print("It's a dog!")
else:
w = raw_input("Is this thing water-based? ").lower()
if w == "yes":
print("It's a boat!")
else:
w = raw_input("Does this thing fly? ").lower()
if w == "yes":
w = raw_input("Does this thing have wings? ").lower()
if w == "yes":
print("It's an airplane!")
else:
print("It's a rocket ship!")
else:
w = raw_input("Is this thing thrown? ").lower()
if w == "yes":
print("It's a baseball!")
else:
print("It's a bicycle!")
sleep(2)
print("Thank you for playing Guessinator!")
game()
sleep(3)
print("This game was very reliant on something called an if/else statement")
sleep(5)
print("It was also very reliant on 'raw_input()' variables")
sleep(5)
print("The 'raw_input()' allows for you, the player, to write and answer")
sleep(5)
print("The 'raw_input' was made into a variable, so it could then run the if/else statements.")
sleep(5)
print("A variable can be anything. It could be a single letter, or even the longest word in the dictionary")
sleep(5)
print("The if/else statements are used to determine what question to ask next after you've answered.")
sleep(5)
print("If you answered yes to an answer, it would then ask a question using the information from the question")
sleep(6)
print("If you answered no, the 'else' part would run, and ask a question knowing it wasn't the thing from the question")
sleep(7)
print("These statements you're reading right now are shown using the print() function and strings")
sleep(6)
print("The string uses quotes to know what text to print")
sleep(5)
print("The quotes are inside the parentheses in the 'print()' statement. It will then print the statement.")
sleep(6)
print("The final thing you should know about is functions.")
sleep(5)
print("Functions use the term 'def' to define a function.")
sleep(4)
print("It will look something like this:\ndef tutorial(): \n words\n code\n text")
sleep(5)
print("You can simply type 'tutorial()'(as an example) into python, and then the function/code will run.")
sleep(7)
print("This has been your tutorial. Thank you for playing and learning.")
sleep(6)
tutorial()
Video lectures
Free with a county library card
https://www.lynda.com/search?q=python
Video lectures, useful exercises, sandbox playgrounds
https://www.codecademy.com/learn/learn-python
Advanced questions, more advanced answers
Directed study in a classical classroom style
Requires invite from instructors. Email engineering@bsmschool.org to request access
https://teamtreehouse.com/tracks/learn-python
This shows how to to make sequences and gives a solution example of euler problem 48
This page has answers to the first few hundred Euler Problems so you can see if your code works.
https://github.com/luckytoilet/projecteuler-solutions/blob/master/Solutions.md
Python Tutorial - Python for Beginners [Full Course]
This 6 hour YouTube tutorial is a in depth guide for beginners trying to learn code. This video explains how to use functions and put them to use at a larger sale. Around the end of the video, you are shown how to create basic machine learning functions, and create a website with python.
Python Intro - Python Course for Beginners
This course is 20 to 40-minute video that will help you gain some knowledge before you start Pi, and this will help you with advent of code if you choose to do that. Corey's style of videos are lots of little videos that go more in-depth on a single or multi topic. try doing 1 a day for the first week of coding and you should be all set.