(CODECHUM) Python Programming Fundamentals: Variables, Data Types, and Libraries

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/27

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

28 Terms

1
New cards

Python

Created by Guido van Rossum (1991). Known for: Simplicity, readability, significant whitespace. Used for web dev, data science, AI, automation.

2
New cards

Basic Structure

Import statements, Functions and classes, Statements/expressions, Comments.

3
New cards

Import Statements

import math, from datetime import datetime.

4
New cards

Syntax Rules

Indentation defines blocks (no braces {}). Variables: no explicit declaration; dynamically typed.

5
New cards

Example of Variable Assignment

x = 10 if x > 5: print("x is greater than 5")

6
New cards

Errors

Syntax Error - invalid code structure. Runtime Error - error during execution. Logical Error - wrong logic, program runs but gives wrong result.

7
New cards

Comments

# Single line comment, """ Multi-line comment (docstring) """.

8
New cards

Variables

Must start with letter/_. Case-sensitive. Cannot use reserved words.

9
New cards

Example of Variable Usage

username = "admin", item_count = 50, _price = 19.99.

10
New cards

Data Types

int, float, str, bool. Collections: list, tuple, dict, set.

11
New cards

Constants

Not enforced but written in UPPERCASE. PI = 3.14159, MAX_SIZE = 100.

12
New cards

Reserved Words

Examples: if, else, while, for, def, return, True, False, None.

13
New cards

Output Operations

print() displays output. \n = newline, end="" to control end character.

14
New cards

Formatted Output Example

name = "John", age = 25 print("Name: %s Age: %d" % (name, age)) print(f"Name: {name}, Age: {age}") # f-string.

15
New cards

Escape Sequences

\n newline, \t tab, \" double quote, \\' single quote, \\ backslash.

16
New cards

Placeholders

%d integer, %f float, %s string.

17
New cards

Example of Print with Placeholders

print("Name: %s Age: %d Height: %.2f" % (name, age, height)).

18
New cards

Input Operations

num = int(input("Enter a number: ")), name = input("Enter name: "), nums = list(map(int, input("Enter numbers: ").split())).

19
New cards

Arithmetic Operators

+ add, - subtract, multiply, / divide, // floor divide, % modulus, exponent. Precedence: > unary +/- > /%// > +/-.

20
New cards

Assignment Operators

= assign, +=, -=, *=, /=, //=, %= update + assign.

21
New cards

Bitwise Operators

& AND, | OR, ^ XOR, ~ complement, << left shift (×2ⁿ), >> right shift (÷2ⁿ).

22
New cards

Example of Bitwise Operations

num1, num2 = 10, 6 print(num1 & num2) # AND → 2 print(num1 | num2) # OR → 14.

23
New cards

Strings

Sequences of characters, immutable. Operations: Concatenation: "Hello" + "World", Comparison: ==, !=, <, >, Copying: new = old.

24
New cards

Implicit Typecasting

Auto conversion x = 10, y = 3.14 print(x + y) # x becomes float.

25
New cards

Explicit Typecasting

Manual conversion x = int("10"), y = float("3.14"), z = str(100), c = chr(65) # 'A'.

26
New cards

String Methods

len(s) - length, s.replace(old, new), s.strip() - remove whitespace, " ".join(list) - join elements, s.startswith(prefix), s.endswith(suffix).

27
New cards

Search Methods

s.find(), s.rfind() (returns index or -1), s.index(), s.rindex() (raises error if not found).

28
New cards

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.