Have you used Python before? (Hint: Raspberry Pi Minecraft?)
Python (and computer programming) basics
Syntax
Data types
Operators
Variables
Syntax
Basically grammar for computers
Someone give an example of a grammatically correct ENGLISH sentence!
Now move around the punctuation and randomize the capitalization and scramble the words
There's a grammar for every language, including computer language -- it's called syntax!
Basic data types
String
Integer
Float
Boolean
Examples?
Hello World!
cd into Documents and make a directory named "python_work" and cd into that
Make a file called "hello_world.py"
Edit the file and type:
print("Hello world!")
Launch your new program in the terminal!
Operators
+, -, *, /, %, <, >
Order of operations anyone?
Open up your Python interpreter and try it out
ex type "python3" into your terminal
>>> 5+5*10
Note: Ctrl+D quits out of the interpreter back into the terminal emulator!
Python excercises
>>> print("Hello world!")
Try to solve this math problem by typing one line into the interpreter:
Suppose you do chores once a week, for which you get $5 pocket money, and you have a paper route which you do 5 times a week and get $30—how much money would you earn in a year?
Variables
In the interpreter type in:
>>> fred = 100
>>> print(fred)
We just created a variable, called "fred"
What is the data type of the variable fred?
A variable is just something that stores value.
A variable varies, and can be reassigned. Try:
>>> fred = 200
>>> john = fred
>>> print(john)
Strings and variables
In Python, if you want a variable to hold a string, you need to put quotes around it
>>> fred = "this is a string"
>>> print(fred)
Now, try this:
>>> fred = "this is two
What did you get?
Try this:
fred = ’’’this is two ... lines of text in a single string’’’
Tricks with strings
Try this:
>>> print(10 * ’a’)
>>> print(20 * ’abcd’)
A "%" can be a placeholder for values:
>>> mytext = ’I am %s years old’
>>> print(mytext % 12)
Try this as well:
>>> mytext = ’Hello %s, how are you today?’
>>> name1 = ’Joe’
>>> name2 = ’Jane’
>>> print(mytext % name1)
Lists
If you wanted to store a shopping list in a variable you could create a string:
This is more typing, but it’s also more useful. We could print the 3rd item in the list by using its position (called its index position), inside square brackets []: