Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python1 Worksheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PYTHON-1- 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 .

We can Join data in print statements with ( + ) sign :


Ex: name = input("What is your name?")
print(name + “ is a nice name”)
2nd lesson:
In programming, pieces of text are called strings, strings are written inside single or double quotes: " " or ' '.

And we can also multiply them ex: print("Hello" * 3) # prints HelloHelloHello.

What happens if we want to print a sentence that has quotes in it ?

If it's single quotes ‘ ’ :

Ex: print("I'm programming in Python!") #We use double quotes around the print
statement.

If it's Double quotes “ ” :


Ex: print(‘Python was named after "Monty Python" ‘) # We use single quotes around
the print statement if we use double quotes gives us an error because Python thinks the
second " is the end of the string:

What if we have both ' and " ?


We can put one type of quotes around the outside of the string, and tell the computer to ignore any more of these by
putting a backslash \ before them. This is called escaping:

1
Ex: print("And the computer said: \"I'm a computer, silly!\"") or

print('And the computer said: "I\'m a computer, silly!"')

There are 2 ways we can print multi-line statements:

Triple " quotes

Ex: print("""Hitting enter here

will work properly""")

The special newline character \n :

Ex: print(“Hitting enter here \n will work properly”)

name = 'Ahmad' # Right now, the value stored inside the variable called name is a string that says 'Ahmad' . So we

can say that name is a string variable.

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

BEDMAS - Order of operations


when you have different operations (+ - / * ) in one equation, they have to be done in a certain order. One
common rule is BEDMAS.

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

If we want to do 3 + 4 first, we need brackets () :

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

variable, if it stores a float, it's a float variable.

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

give you 2.0 .

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:

print("This week you have earned: $", wages)

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

always a space between them and sometimes we don't want a space.


Python has a built-in function called .format() which gives us more control over how our output is printed on the

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.

Usually we try to keep comments short, simple and necessary.

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,

with underscores _ separating words ex: first_name

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)

You might also like