- The difference between python 2 and 3 is that three has parenthesis and two doesn't
- Text in pyhon is called STRING
- Errors are from when the computer doesn't understand what you want it to do
- Use += syntax to add value to an existing variable
- Numbers with decimal points are called floats
- Dot notation only work with strings
g = "Golf"
h = "Hotel"
print "%s, %s" % (g, h)
For interviews; intro to data structures and algorithms
you can also use the website called leetcode
==equal to
!= not equal to
< less than
<= less than or equal to
greater than
=greater than or equal to
Please note that = assigns a value to a variable
And returns TRUE when the expression on both sides of and are true
Functions are defined with three component;
1. The header
2. The comment which is sometimes optional
3. And the body
HOW PREVENT ERRORS
- Backup your code frequently
- Run your program frequently, it helps you know when the error occurred
- Prevents you from saving a backup that doesn't work
WHAT ARE FUNCTIONS
My calculate reuses the same 10 lines of code 20 times
- It is a question of, how can we reuse code to make a program space efficient and easy to read?
- It includes, print statements, for loops, basic math operators.
- Definition: a segment of code that can easily be run by calling out a function then it may do something in return.
- Can be called in numerous times and in numerous places
- It is like wrapping code in a present and giving it a name. Which when called will unwrap the present and bring what's inside. E.g. print("hello")
**THE USE OF FUNCTIONS**
- To recycle sections of code that serve the same purpose.
- To save space
- Used for equations you want to allow multiple inputs of.
**TYPES OF FUNCTIONS**
- Functions can be divided into two:
1. Whether or not they return a value,
2. And whether or not they take in an argument.
Arguments are variables we pass into a function in order to be manipulated and either
- Returned back to us
- Printed in a console
- Used in another operation
In real life: If i walked into cold stone, and said "I want to order ice-cream"(function) they would probably be confused. What type of ice-cream would I like because they have multiple flavors. So I say "I want chocolate chip cookies with rainbow sprinkles." (argument) Then I would have an argument (type of ice-cream I ordered) returned back to me
Argument can be many things, as long as it is in the menu e.g. vanilla, caramel, cheese biscuit.
- In programing Arguments can be strings, integers, arrays etc. pretty much anything.
EXAMPLE: MAX FUNCTION ARGUMENT
Takes in two arguments (two integers) and returns the higher one
Int maxNumber= Max (1,100);
maxNumber=100
If you don't put two numbers or integers for it to compare, it reads null
Int maxNumber = Max( Two, Steve);
maxNumber=Null
Variables diversify your code. For example, if you were in a restaurant that offers one type of food, that would not be so useful
But if they haD a variety to choose from, then you would be able to tell them what you actually want.
**
FUNCTIONS WITHOUT ARGUMENTS**
Example, if you were making a system that allows players to "press 5" each time they wanted to view stats, instead of writing down all the stats compilation, you could just write " printstat() and everytime someone pressed 5 they could view stats without a problem
- Functions are super useful for making large changes to your code easily.
- Changing a function will change all the future calls of that function.
HOW CAN WE IMPORT FUNCTIONS
I don't want to code 1000 functions for my code.
- You could plant your own tree and make your own paper, but why do that when you could just walk into a bookshop and buy a book?
- Importing functions allows you to access libraries of premade functions
- What are libraries: a collection of functions that all have the same theme. Eg math library, data analysis library etc.
- How do we import: using import statements.
- In most languages, an import statement consists of three parts.
- The library you would like to import from
- The package you would like to import
- Which class from that package you would like to use.
- Example if you wanted to import sth from python and java
- Python would read…from math import factorial
- Java would read … import java.math.factorial
**
HOW TO MAKE OUR OWN FUNCTIONS**
- WHAT do we do when we cannot find a function online that suits our needs?
**
FUNCTION NAMING CONVENTIONS**
- Functions naming conventions follow variable naming conventions.
- Can't be two words.
- Follow the camelCase strutcture (not capitalizing the first word, but capitalizing the second)
FUNCTIONS THAT TAKE IN NO ARGUMENTS AND RETURN NO VALUES
- Each language differs in how you tell the computer to create a fucntion.
- In python you write the word def shortform for define and then you write
Eg def printStat ():
Code goes here
Code goes here
WHAT ARE ARRAYLISTS AND DICTIONARIES
- What can we use if Arrays don't fit the program we are trying to make?
- Arrays are list of values that are stored together.
IMPORTANT TO NOTE
- Before writing code, you will need to think of the code.
- You can either use a flowchart or you could use a pseudocode.
- There are three types of pseudocodes.
1. Flowchart method- good for thinking through the flow of a function.
2. Write-Up method- good for getting the general idea of a program down.
3. Functionality planning method- good for listing down the functions of a certain program.
For practice you can use websites like codingbat and coderbyte
Interactive websites freecodecamp and codecademy
Top comments (0)