Python1 Worksheet
Python1 Worksheet
Python1 Worksheet
1st lesson:
In programming texts are called strings and we wrap them in quotes (" ") , We can
also print numbers and calculations. When we print numbers, we still need the brackets
( ) but we don't need the quotes " " .
Ex: print("Hello, world!") # This code prints the message Hello, world!
print(“2 + 2”) #Prints the messege 2+2
print(2 + 2) #Prints the number 4
We can make a program by asking the user questions and printing their answers. To do this we
use an input statement and a thing called a variable to store their response, The following
code asks the user's age and stores it (using = ) in a variable called age:
Ex: age = input("What is your age?") We can print variables just like strings and numbers:
print(age)
We don't need quotes " " around a variable name, and in Python we write variable names
in lower_case .
Ex: print("I'm programming in Python!") #We use double quotes around the print
statement.
1
Ex: print("And the computer said: \"I'm a computer, silly!\"") or
name = 'Ahmad' # Right now, the value stored inside the variable called name is a string that says 'Ahmad' . So we
Variables are called variables because the values they store can change during a program.
3rd lesson:
Let's take a closer look at math in Python. Here are the main math operators:
+: Addition -: Subtraction
*: Multiplication /: Division
And we also have ** for powers or exponents. For example, 23 would be written as 2 ** 3
This means that anything inside Brackets ( ) is calculated first, followed by Exponents (or powers, ** ), followed
by Division / and Multiplication * , then Addition + and Subtraction - :
print(3 + 4 * 2)
In the above print statement the computer multiplies 4 * 2 first to get 8 , and then adds 3 . This gives 11
print((3 + 4) * 2) #prints 14
There are two main types of numbers we'll see at this level: integers and floats.
Integers are whole numbers like 1, 56, -32 and so on.
2
Floating point numbers or floats have a decimal point in them like 3.2 or 0.1.
We identify what the datatype of a variable is by what is stored inside it; if it stores an integer, it's an integer
NOTE: One important thing to know is that if you divide in a calculation, you'll always get a float back, so 8 / 4 will
4th lesson:
We can add two strings together with the + operator , We can also add variables that have strings stored in them ,
But What happens with numbers? It gives error if we use + because one is a number and one is a string–they have
different datatypes.
One simple way that we can combine strings with numbers or calculations is to use a comma (,) like this:
BUT This will print a space between the two parts automatically.
A comma (, ) is not always the best way to combine strings and numbers in a print statement, because there is
screen.
Here is how .format() is used
Ex: print("9 * 3 = {}".format(9 * 3)) # the result inside format () goes to the bracket { } where we write after = sign
5th lesson:
In this lesson we are going to take a look at comments and some other conventions for writing Python.
Conventions in coding are basically a set of guidelines that everyone agrees on to make it easy to understand code.
Comments are designed to help humans understand what is going on and why.
Here's an example:
#Set hourly_rate equal to 15
hourly_rate = 15
Choose good variable names, There are some guidelines to choose a good variable names:
1. Avoid single letter variable names ex: a = 15
2. Use standard, descriptive names ex: hourly_rate it can be better than rate.
3
Variable naming conventions, Here are some more guidelines for well-named variables:
1. Variable names can't start with a number ex: 9number = 9
2. Variable names should be written as lower_with_under which means all lower case,
Punctuation conventions, Python has guidelines for all that punctuation too!
1. Don't put spaces before or after brackets ex: print ( "Hello, world!" )
2. Do put spaces between numbers and operators in calculations, on either side of equals= , and after
commas, EX: print("2 + 2 - 1 =", 2 + 2 - 1) NOT print("2+2-1=",2+2-1)
3. Don't use brackets when you don't need them: Ex: print((3 * 21) + 4) it can be print (3 * 21 +4)