1/27
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Python
Created by Guido van Rossum (1991). Known for: Simplicity, readability, significant whitespace. Used for web dev, data science, AI, automation.
Basic Structure
Import statements, Functions and classes, Statements/expressions, Comments.
Import Statements
import math, from datetime import datetime.
Syntax Rules
Indentation defines blocks (no braces {}). Variables: no explicit declaration; dynamically typed.
Example of Variable Assignment
x = 10 if x > 5: print("x is greater than 5")
Errors
Syntax Error - invalid code structure. Runtime Error - error during execution. Logical Error - wrong logic, program runs but gives wrong result.
Comments
# Single line comment, """ Multi-line comment (docstring) """.
Variables
Must start with letter/_. Case-sensitive. Cannot use reserved words.
Example of Variable Usage
username = "admin", item_count = 50, _price = 19.99.
Data Types
int, float, str, bool. Collections: list, tuple, dict, set.
Constants
Not enforced but written in UPPERCASE. PI = 3.14159, MAX_SIZE = 100.
Reserved Words
Examples: if, else, while, for, def, return, True, False, None.
Output Operations
print() displays output. \n = newline, end="" to control end character.
Formatted Output Example
name = "John", age = 25 print("Name: %s Age: %d" % (name, age)) print(f"Name: {name}, Age: {age}") # f-string.
Escape Sequences
\n newline, \t tab, \" double quote, \\' single quote, \\ backslash.
Placeholders
%d integer, %f float, %s string.
Example of Print with Placeholders
print("Name: %s Age: %d Height: %.2f" % (name, age, height)).
Input Operations
num = int(input("Enter a number: ")), name = input("Enter name: "), nums = list(map(int, input("Enter numbers: ").split())).
Arithmetic Operators
+ add, - subtract, multiply, / divide, // floor divide, % modulus, exponent. Precedence: > unary +/- > /%// > +/-.
Assignment Operators
= assign, +=, -=, *=, /=, //=, %= update + assign.
Bitwise Operators
& AND, | OR, ^ XOR, ~ complement, << left shift (×2ⁿ), >> right shift (÷2ⁿ).
Example of Bitwise Operations
num1, num2 = 10, 6 print(num1 & num2) # AND → 2 print(num1 | num2) # OR → 14.
Strings
Sequences of characters, immutable. Operations: Concatenation: "Hello" + "World", Comparison: ==, !=, <, >, Copying: new = old.
Implicit Typecasting
Auto conversion x = 10, y = 3.14 print(x + y) # x becomes float.
Explicit Typecasting
Manual conversion x = int("10"), y = float("3.14"), z = str(100), c = chr(65) # 'A'.
String Methods
len(s) - length, s.replace(old, new), s.strip() - remove whitespace, " ".join(list) - join elements, s.startswith(prefix), s.endswith(suffix).
Search Methods
s.find(), s.rfind() (returns index or -1), s.index(), s.rindex() (raises error if not found).
Math Functions
import math print(math.sqrt(16)) # 4.0 print(math.pow(2, 3)) # 8.0 print(math.sin(0.5)) # 0.48 print(math.cos(0.5)) # 0.88.