1/61
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Python
A high-level, readable programming language.
Purpose of Python
To write programs for automation, web development, data analysis, etc.
Example of Python code
print("Hello, world!")
Variables
Containers for storing data values.
Purpose of Variables
To store and reuse values throughout a program.
Example of Variable
x = 10
Types
Classifications of data (int, float, str, etc.).
Purpose of Data Types
To define what kind of data is being used.
Example of Data Types
age = 18 (int), name = "Sam" (str)
Branching
Allows decision-making using conditions.
Purpose of Branching
To execute different code paths based on logic.
Example of Branching
if x > 10:\n print("Big")
While Loop
Repeats a block of code as long as a condition is true.
Purpose of While Loop
Useful when the number of iterations isn't known.
Example of While Loop
while x < 5:\n x += 1
For Loop
Iterates over a sequence (like a list or range).
Purpose of For Loop
To repeat tasks a known number of times or over collections.
Example of For Loop
for i in range(5):\n print(i)
Functions
Reusable blocks of code with a name.
Purpose of Functions
To organize code and perform tasks multiple times.
Example of Function
def greet():\n print("Hi!")
Strings
Text data enclosed in quotes.
Purpose of Strings
To store and manipulate text.
Example of String
msg = "Hello"
Lists / Dicts
List: ordered collection; Dict: key-value pairs.
Purpose of Lists / Dicts
To store multiple items in a single variable.
Example of List and Dict
fruits = ["apple", "banana"], person = {"name": "Alice"}
Classes
Blueprint for creating objects (OOP).
Purpose of Classes
To model real-world entities and use object-oriented principles.
Example of Class
class Dog:\n def init(self, name):\n self.name = name
Exceptions
Errors caught and handled during execution.
Purpose of Exceptions
To make code more robust by handling errors gracefully.
Example of Exception handling
try:\n x = 1/0\nexcept ZeroDivisionError:
Modules
Files containing Python code (functions, classes, etc.).
Purpose of Modules
To organize and reuse code across files.
Example of Importing a Module
import math\nprint(math.sqrt(4))
Files
Input/output operations on text/binary files.
Purpose of Files
To read from or write to external files.
Example of File Operation
with open("file.txt") as f:\n print(f.read())
Plotting
Visual representation of data (often using libraries).
Purpose of Plotting
To analyze and visualize data.
Example of Plotting
import matplotlib.pyplot as plt\nplt.plot([1,2,3],[4,5,6])\nplt.show()
Python Introduction
Python is used for algorithms and machine learning.
Python Execution Model
Python is both compiled and interpreted.
Syntax Error
Occurs when the program contains invalid code that can't be understood.
Identifiers
Variable names that cannot use reserved words.
Arithmetic Operators
Normal division keeps remainder (/), floor division removes it (//).
Python Core Data Types
Lists, integers, strings, dictionaries, tuples, boolean, sets.
Purpose of Len
Counts the number of elements or characters in a list or string.
Negative Indexing
Starts with 1 and counts down instead of starting from 0.
Insert Method (lists)
Assigns an index to a value in a list.
Example of Insert Method
my_list.insert(2,67) replaces index 2 with the value 67.
Tuples
Cannot be updated or changed; store finalized data.
Updating a Tuple
Convert tuple to list first, then update.
Dictionary Deletion
Use del to remove an item from a dictionary.
Example of Dictionary Deletion
del d1["John"]
Retrieval from Dictionary
Use d1.get("Susan") to get a value.
Check Membership in List
Use syntax 15 in my_list to check if value exists.
If Statements
No else required, but else needs a corresponding if.
Blocks in Python
Represented by indentation.
Assignment vs Equality
= is for assigning notations, while == is for equality checks.
If Statement Execution
Executes code below if condition is true, else moves on.