Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python Notes

Uploaded by

Haashir Tanveer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Notes

Uploaded by

Haashir Tanveer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Notes

List of Useful String Functions

.lower - converts the string to lowercase characters


.upper - converts the string to uppercase characters

.islower - checks if the string is lowercase and returns a boolean value


.isupper - converts the string is uppercase and returns a boolean value

len(string_name) - returns the length of the string

string_name(index) - returns the character in the string at the specified index

string_name(character) - returns the index of a character in the string (will give an


error if the character is not in the string)

string_name.replace(character being replaced, character being added) - replaces a


character/word with another character/word

List of Useful Number Functions

str(num) - converts a number to a string (numbers can’t be printed next to strings)

abs(num) - returns the absolute value of a number

pow(base, exponent) - exponent function (or **)

max(num 1, num 2) - returns the larger of the two numbers


min(num 1, num 2) - returns the smaller of the two numbers

round(num) - rounds to the nearest whole number

- The following functions need a math library to work:


from math import *

floor(num) - rounds down to a whole number


ceil(num) - rounds up to a whole number

sqrt(num) - finds the square root of a number


Getting Input
input()

- String statements can be added in the brackets


input("Enter your name")

- Can also be assigned to variables


name = input("Enter your name")

Data Types

- Python is a dynamically typed language meaning that data types do not need
to be specified when declaring a variable

Ex:

- x is automatically interpreted as an int upon declaration


x = 5

- variable data types can be specified when needed using ”:”


x:int =5

Lists

Note: Strings in python are immutable, lists are not.

- Similar to arrays in other languages


- Can be printed as a whole using print() instead of using a for loop
- len() function can be used for lists as well

Syntax to define a list:


items = ["ball", "bat", 5, "helmet", "gloves", 7]

Useful Functions:

.append() - adds an element to the end of the list (+ can be used instead)
..remove() - removes specified element from the list
..pop() - removes the last element and returns that value
.clear()
.insert()
Nested Lists
- Lists can be nested within each other and treated as elements
Ex: items = ["Ball", "Bat", ["Gloves", 7, "Hat"]]
- ["Gloves", 7, "Hat"] , is a list nested within items and can be referred to as
any other element
- 7 can be accessed using the following syntax
items[2][1]
- 2 is the index of the list, and 1 is the index of 7 within that list

Negative Indexes

- In python lists also have negative indexes that start at -1 at the last element
and decrement by 1 moving backwards in the list

- Can be useful if the length of the list is not known

Copying Lists

- In python, when you set a list to equal another list, you are simply creating an
alias for that list.
- Ex:
- items = ["Ball", "Bat", "Gloves", 7, "Hat"]
- copy = items
- In this case copy is simply an alias for items and any changes to copy will also
affect items

- Instead, a shallow copy can be made in two ways:


copy = items.copy()
copy = items[:]
- Now, copy and items are two different lists and changes to one will not affect
the other
Note:
- When nested lists are modified in a shallow copy, the original list will also be
modified because both the original list and the copy will point to the same
location in memory for the nested list

A deep copy can be made to avoid this using the following syntax:

import copy
items = ["Ball", "Bat", ["Gloves", 7, "Hat"]]
c = copy.deepcopy(items)

Changing Data Types

type() - returns the data type of a specific character or variable


int() - changes a number or variable to type int
float() - changes a number or variable to type float
str() - changes a character or variable to type string

If Statements

If statement syntax:
if 1 + 1 == 2:
print("Hello")
elif 1 + 1 == 1:
print ("Bye")
else:
print("Something")

- Python uses indents instead of curly braces


- The indented code is the code which is interpreted as being in the if statement
(proper convention is 4 spaces, but visual studio replaces tab with 4 spaces)

&& and || operators in other languages are represented by and & or respectively
Not Operator vs !=

!= operator “Is not” operator

The != operator compares only the The “is not” operator compares if the

value of the objects being objects are pointing to the same memory

compared. location or not.

It returns True if the value of both It returns true if the objects are not

the objects are different and False pointing to the same memory location

otherwise. otherwise it returns false.

- == and != check for equivalency, while is and is not check if the objects being
compared are identical or not.

Flow Charts

Parallelogram - Used to indicate input/output


Rectangle - Used to indicate processes (operations/calculations)
Diamonds - Used to indicate Branching
Rounded Rectangles - Indicate where the program starts and stops

Recursion
Base Cases: Cases that require no “work” to compute
Recursive Cases: Simplify - > Recursive Call - > Do Additional Work

Dictionaries
#initializing dictionary
phonetic_dictionary = {}

#"a" is the key, "alpha” is the value


phonetic_dictionary["a"] = "alpha"

Multidimensional Lists:
- A list of lists
- Can be accessed using two square brackets
- First square bracket is the index of the row
- Second square bracket is the index of the column

Graph Theory:
- Tuple of two collections
- FIrst element is the thing of interest (Vertices)
- Second element is the relationships with the Vertice (Edges)

You might also like