Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Vey Easy Python Summary

Rating
-
Sold
1
Pages
19
Uploaded on
19-06-2021
Written in
2020/2021

Notes for anyone who is starting to learn python or moving from other languages to python. These are basic to intermediate-level notes. Advanced version will be coming soon

Institution
Course

Content preview

Python Programming Notes
Created by: Eddie Krasinski
Black: Usual Notes
Red: What you’d actually put in Python file

Introduction

Here are some notes that I’ve been taking over the past few weeks as I learn Python
using learnpython.org. I recommend going through this site as well, as they give example
problems for each of the sections listed below. My background in programming lies in C and
MATLAB, and unlike these functional programs, Python is object oriented, which will be hard to
grasp as first. In functional programming like C and MATLAB, every single variable you use must
be defined beforehand, but in Python, variables can be created on the spot without prior
definition (mainly in loops), as long as it corresponds to some sort of data (string, values) which
follows the object’s declaration. You’ll see what I mean as you go through these notes. You
could still define all variables beforehand, as you would in MATLAB, but that’s not the best use
of Python. To access all of Python’s built in functions, visit https://docs.python.org/3/library/.

Common Differences between Python and MATLAB, ways to approach Python
o In MATLAB %% is used for comments, in Python, # is used
o Don’t need to use “end” in loops in Python, does this automatically with spacing
▪ Put a colon after each line containing a loop/Boolean statement
o Spacing is very important in loops, functions, classes created in Python, use tabs
to avoid trouble
o MATLAB’s command window= Python’s shell
o Any file ending with .py= A MATLAB Script
o DataFrames in Python are kind of like an Excel table, rows and columns of data
o /n used for new line
o Input() is used to call an element from the user
o / is used to continue current line on the next line (… in Matlab)

Variables and Types
Strings can be added to form a new object, such as
H=”hello”
W= “world”
helloworld= H + “” + W

Can assign multiple variables by separating with a comma
a,b=3,4
print(a,b)

cannot mix strings with numbers, ex
one = 1
helloworld= h + one + W

,Lists
.append adds an object to a list in order
Position 0 is actually the first character
For x in mylist:
Print(x)
#this prints out everything in the list

Can also sort through lists like:
A=[1,1,2,3,6,4,8]
B=[2,45,6,3,4]
C=[A*B for element in A for element2 in B if element==element2]
To access certain entries in a list, use brackets:
D=[A[0], A[4:6]]

Arithmetic Operators
%: modulo operator, returns the remainder after division, 11%3 returns 2
** 2: The same as squared
** 3: The same as cubed
Can multiply strings: “hello”*10 gives 10 iterations of hello
Can join lists with addition operator (but can’t mix values with strings)
Can multiply lists, list*3= listlistlist

String Formatting
Print(“hello %s!” % name)
%s can step in for strings and lists
Print(“%s is %d years old” % (name, age))

%d is for integers
%f is for floating point
%.number of digitsf: floating point with specific number of placeholders
%x/%x: Hex representation (integer lowercase/uppercase)

String Operations
Len(string) gives # of characters inside string
.index(“o”) gives place of character, in this case the letter o
String[3:7] gives characters between 3rd and 7th place
String[3:7:2] gives every other character between 3rd and 7th place
String[::-1] returns the string but backwards
.upper converts all characters of string to uppercase
.lower converts all characters of string to lowercase
.startswith(“object”) returns true if string starts with that object, false if not
.endswith(“object”) same as above
.split(somecharacter) creates a list with multiple entries, split at the character chosen

, “**”.join(somelistofstrings) will print string1**string2**string2

Conditions
If, and, or is used (if blah and blah then blah)
Is True or is False used
Elif… else # always end line containing condition with colon
== #equate things for Boolean condition
< or > or <= or >= #greater or less than for Boolean condition
In checks if specified object lies within iterable object container
is does not match values of variables, it checks instances, therefore different than ==
Not operator inverses following Boolean phrase


Loops
Don’t need to put an end on for loops (unlike MATLAB)
Range(3,6) returns a new list of 3,4,5
Xrange(3,6) returns an iterator of 3,4,5
While loop is normal (same as C and MATLAB)
!= is not equal to
Count+=1 is the same as count=count+1
Break used to exit for or while loop
Continue used to skip current block and return to for or while loop
Else can be used as a loop(Unlike C/MATLAB), if break inside for loop, else part is
skipped, else part continued even with continue statement
Can access certain points in a list using iterations, such as:
For i in mylist:
If mylist[i]==2:
Print(“This element is equal to 2”)


Functions
Called using def myfunction(args) (myfunction can be any name)
Use return under def to return a value, string, to the caller
To call the function, after defining, just put myfunction(args) in same script
Can return strings like print statements “%s is a good fit for functions” % benefit

Reading, opening, writing to Text Files
Always convert objects to strings before writing to text files. Always close the text file
once you are done editing or reading it. Lastly, if you have a file with the same name, and you
write into the file with “w”, Python will eliminate the original data on that file and replace it
with whatever you are appending to the file.

with open('file_to_save.txt', 'w') as open_file:
open_file.write('A string to write')

Written for

Institution
Course

Document information

Uploaded on
June 19, 2021
Number of pages
19
Written in
2020/2021
Type
SUMMARY

Subjects

$20.95
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
jitishb

Get to know the seller

Seller avatar
jitishb Trinity College Dublin
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
4 year
Number of followers
1
Documents
2
Last sold
4 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions