












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
All the needed notes for this class.
Typology: Summaries
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Python is a high-level programming language that is:
โ Easy to read โ Easy to learn โ Interpreted (runs line by line) โ Used for web development, data science, artificial intelligence, automation, and software development
Example:
print("Hello World")
Output:
Hello World
Variables store information.
Example:
name = "Kyle" age = 19 height = 5.
Rules:
โ Cannot start with a number โ Cannot contain spaces โ Case sensitive
Valid:
student_name age x
Invalid:
2age student name
Whole numbers
x = 5 y = -
Decimal numbers
price = 19. pi = 3.
Text
name = "Kyle" course = "CSC108"
Example:
name = input("Enter your name: ")
print(name)
Input always returns a string.
Example:
age = int(input("Age: "))
price = float(input("Price: "))
Addition
โ
Example:
5 + 3
Output:
8
Subtraction
โ
Example:
5 - 3
Output:
Multiplication
โ
Example:
5 * 3
Output:
15
Division
/
Example:
10 / 2
Output:
Floor Division
//
Example:
10 // 3
Output:
3
Modulus (Remainder)
== Equal
!= Not Equal
Greater Than
< Less Than
= Greater Than or Equal
<= Less Than or Equal
Example:
5 > 3
Output:
True
and
Both conditions must be true.
Example:
x > 0 and x < 10
or
At least one condition must be true.
Example:
x < 0 or x > 100
not
Reverses a boolean value.
Example:
not True
Output:
False
Used for decision making.
Example:
age = 18
if age >= 18: print("Adult")
Example:
age = 16
if age >= 18: print("Adult") else: print("Minor")
x = 1
while x <= 5: print(x) x += 1
Output:
1 2 3 4 5
Repeat a specific number of times.
Example:
for i in range(5): print(i)
Output:
0 1 2 3 4
Common Range Forms
range(5)
0 to 4
range(1,6)
1 to 5
range(0,10,2)
0 2 4 6 8
Example:
name = "Python"
Length
len(name)
Output:
6
Indexing
name[0]
Output:
P
name[1]
Output:
y
Negative Indexing
name[-1]
Output:
Removing Elements
numbers.remove(3)
Length
len(numbers)
Functions allow code reuse.
Example:
def greet(): print("Hello")
greet()
Function with Parameters
def greet(name): print("Hello", name)
greet("Kyle")
Output:
Hello Kyle
Return Statement
def square(x): return x * x
result = square(5)
Output:
Global:
x = 10
def test(): print(x)
Local:
def test(): y = 5
y only exists inside the function.
Example:
numbers = [1,2,3,4]
for num in numbers: print(num)
Output:
1 2 3 4
student["name"]
Output:
Kyle
Like lists but cannot be modified.
Example:
coordinates = (10,20)
Writing to a file
file = open("data.txt","w")
file.write("Hello")
file.close()
Reading a file
file = open("data.txt","r")
content = file.read()
file.close()
Syntax Error
Missing colon, bracket, etc.
Name Error
Using a variable that doesn't exist.
Type Error
Using incompatible data types.
Example:
"5" + 5
Index Error
Accessing an index that doesn't exist.
Measures efficiency.
O(1)
Constant time
Example:
list[0]
O(n)
Linear time
for i in range(5): code
While Loop:
while condition: code
Function:
def function_name(): code
List:
numbers = [1,2,3]
Dictionary:
data = {"key":"value"}
Length:
len(object)
Add to List:
append()
Return Value:
return value
Most Important Topics for Intro Python Exams