So you're looking to learn Python, here are some notes that will help you to getting started with Python. By the way, if you want to see videos to get started with python you can check them at my YouTube Channel called Just Program, here is the first video in the series So, here are the notes!
What is computer programming - Computer programming is a way to give instructions to a computer to do something
To do computer programming, you write something called code, which is the way that instructions are given to a computer
Code is nice because it isn't 0s and 1s(Binary)
Why learn Python - Python is a great programming language because it is beginner friendly and readable, but is also
really powerful.
Installing - Download python for your specific machine specifications at https://python.org and download a text editor to
code in. I would not recommend using an IDE(integrated development environment) because the beginner should learn the
syntax and learn how to figure our errors instead of letting the IDE do it for them. Download a text editor like
Sublime Text, Atom, or Vim
Data Types - A data type is a type a variable is. Think of a variable as something that stores a piece of information
There are many Data Types, the main ones are Strings, Integers, Floats, and Booleans.
Strings, anything in quotes. Generally a word, but anything else in quotes are strings. ex: "String", "Joe", "6"
Integers, any number that is not a decimal. Not in quotes or anything like that. ex: 1, -5, 78
Floats, any number, decimal or non-decimal. Not in quotes or anything like that. ex: 3.1459, 2, 78.4
Booleans, True or False. HAS TO BE CAPITAL T AND CAPITAL F
1st Program - Create a new folder where you wanna code in, and create a file called first.py
You HAVE to have a .py extension at the end of the file name because all python code is written in .py files.
To test your python installation, write print("Hello")
in first.py You can probably guess this will spit out
Hello in the console when we run this. To run a python program, you do python programname.py on windows or
python3 programname.py on Mac/Linux.
Variables - Variables are something that store a piece of information. The syntax is varName = whatYouWantStored
The variable name cannot start with a number and the convention is to use camelCase or under_scores for variable
names.
Operators - Operators are things like +, which adds numbers, or -, which subtracts numbers, here's a list of them.
+, adds numbers, Syntax: print(9 + 5 + 4)
-, subtracts numbers, Syntax: print(4 - 3)
, multiplies numbers, Syntax: print(8 * 5)
/, divides numbers, Syntax: print(100 / 10)
*, to the power of, Syntax print(8**2)
(will return 64)
%, gets remainder of division problem, Syntax: print(10 % 4)
(will return 2)
Lists - literally lists of elements, can have different data types in them
Syntax: ["Orange", "Apple", "Banana", 3]
list.append(elementToAdd)
- Appends an element to the end of a list
list.insert(index, elementToAdd)
- Appends an element to a specific index in a list
list[index]
- Gets element of a list at a specific index
del list[index]
- Deletes an element of a list at a specific index
If statement - if something do this
Syntax:
x = "Hi"
if x == "Hi":
print("Hello")
elif x == "Bye":
print("Goodbye")
else:
print("Something")
For Loops: Loops over something. Can loop over many things, commonly used to loop over strings and lists
Syntax:
x = ["Tomato", "Onion", "Lettuce"]
for food in x:
print(food)
While Loops: While something is true, do a specific block of code. If something always true, never turns into false,
it results in an infinite loop(which is bad)
Syntax:
x = "Something"
y = True
while y:
if x == "Something":
y = False
Functions: A block of code that does something. Generally used when a lot of code is repeated.
Return keyword returns something from a function, think of whatever is being returned as a replacement to the
function call.
Syntax:
def funcName():
return "Hi"
print(funcName())
This prints Hi because Hi was returned. Convention is to use camelCase in function names.
Function parameters: Information that can be passed into a function. Argument is the same thing as a parameter, except
it's called an argument when function is being called.
def funcName(funcParameter1, funcParameter2):
return funcParameter1 + funcParameter2
print(funcName(1, 8))
This will return 9. In this case, 1 and 8 are the arguments and funcParameter1 and funcParameter2 are the parameters.
Read from a file - You generally use with keyword because you don't have to deal with closing a file and all of that.
You use the open function to open a file, readline function to read first line from the file, and readlines function
to read all the lines of a file. The second argument in the open function is referring to what mode the file will
be opened in, reading from a file = r mode.
Syntax:
with open("file.txt", "r") as f:
for line in f.readlines():
print(line)
Write to a file - You generally use with keyword because you don't have to deal with closing a file and all of that.
You use the open function to open a file, write function to write to the file. You use the open function to write
to a file even if the file doesn't exist as in write or append mode, it creates the file if the file doesn't
already exist. Also, write mode overwrites what's already in the file, append mode appends on to the end of the file.
w = write mode, a = append mode. \n = new line
Syntax:
with open("file.txt", "a") as f:
f.write("This is appended to the end of the file!\n")
Modules: Way to separate different functionality of your project. You can also use modules that other people created
like Tensorflow, or PyGame.
Ex:
calculator.py -
def calc(x, y):
return x + y
main.py -
from calculator import calc
a = int(input("Enter Number: "))
b = int(input("Enter Number: "))
print(calc(a, b))
Optional Parameters - Parameters in functions that are optional. In this example, x and y default to 1 if you don't
enter them, but if you do, they will be whatever you entered
Ex:
def something(x=1, y=1):
return x*y
String Concatenation: Way to combine strings. You can combine strings my doing string1 + string2
. You can repeat a
string by doing string1 * 5
, which is the same as string1 + string1 + string1 + string1 + string1
. CANNOT ADD
STRING AND NUMBERS.
F Strings: A much better way for string concatenation. You can even put numbers in strings. You put variables/operations
in curly braces.
Ex WITHOUT F STRINGS:
x = "69"
y = "43"
print("The value of x is " + x + " and the value of y is " + y + ".")
Ex WITH F STRINGS:
x = 69
y = 43
print(f"The value of x is {x} and the value of y is {y}.")
So yeah, that is the notes I have for anyone who wants to get started with Python!
Top comments (0)