CBSERanker

Loading

PYTHON FUNDAMENTALS

PYTHON FUNDAMENTALS

Python Character Set

A set of valid characters that Python can recognize, including letters (A-Z, a-z), digits (0-9), special symbols (space, +-*/()~’!@#$%^&[]{};;;'”<>/?), white spaces (blank space, Enter, Tab), and other characters (ASCII and UNICODE).

TOKENS

The smallest individual units in a program, including:
– Keywords
– Identifiers (Names)
– Literals
– Operators
– Punctuators

KEYWORDS

Reserved words with special meanings in Python, such as: and, del, from, not, while, as, elif, global, or, with, assert, else, if, pass, yield, break, except, import, print, class, exec, in, raise, continue, finally, is, return, def, for, lambda, try.

IDENTIFIERS

Names given to program parts like variables, objects, classes, etc. Rules:
– Can be a sequence of letters and digits.
– Must start with a letter or underscore.
– Case-sensitive.
– Cannot be a keyword.
– No special characters except underscore.
– No spaces allowed.

Examples of valid identifiers: GradePay, File_12_2018, JAMES007.
Examples of invalid identifiers: Grade-Pay, 12_2018_File, $JAMES007.

LITERALS / VALUES

Data items with fixed values, including:
– String Literals: Enclosed in single or double quotes (e.g., “Python”, ‘123456’).
– Numeric Literals: Integers (e.g., 1234, -50), floating-point (e.g., 12.0, -15.86), and complex numbers (e.g., 1+0j).
– Boolean Literals: True or False.
– Special Literals: None.
– Literal Collections: Lists, tuples, etc.

STRING LITERALS

Can be single-line (enclosed in single/double quotes) or multiline (using backslash or triple quotes). Escape characters (e.g., \n, \t) are used for special actions.

NUMERIC LITERALS

– Integer Literals: Decimal (1234), octal (0o10), hexadecimal (0xF).
– Floating-point Literals: Fractional (12.0) or exponent form (0.105E02).

BOOLEAN LITERALS

Only two values: True or False.

SPECIAL LITERAL None

Indicates absence of value, similar to NULL in other languages.

COMPLEX NUMBERS

Made of real and imaginary parts (e.g., 1+0j, 9-5j). Access parts using .real and .imag.

TYPE CONVERSION

– Implicit: Automatically done by Python (e.g., int to float).
– Explicit: Done using functions like int(), float(), str(), bool().

INPUT AND OUTPUT

– input(): Reads user input as a string. Convert to numeric types for calculations.
– print(): Displays output. Customize with sep (separator) and end (line ending) parameters.

OPERATORS

– Unary: +, -, ~, not.
– Binary: Arithmetic (+, -, *, /, %, **, //), bitwise (&, ^, |), relational (<, >, <=, >=, ==, !=), logical (and, or, not), assignment (=, +=, -=, etc.), membership (in, not in), identity (is, is not).
– Shift: <<, >>.

EXPRESSIONS AND STATEMENTS

– Expression: Legal combination of symbols representing a value (e.g., A+10).
– Statement: Programming instruction (e.g., print(“Welcome”)).

COMMENTS

– Single-line: # comment.
– Multiline: Using triple quotes (“””comment”””).

FUNCTIONS

Blocks of code defined with def, reusable by name. Example:
def drawline():
    print(“=====================================”)
drawline()

VARIABLES

Named locations storing values. Python uses dynamic typing (variables can change type). Example:
x = 100  # int
x = “KVians”  # now string

MULTIPLE ASSIGNMENTS

Assign multiple values at once:
a, b, c = 11, 22, 33
a = b = c = 50

OUTPUT WITH print()

Customize output using sep and end:
print(“Name”, “Age”, sep=”:”, end=” “)
print(25)  # Output: Name:Age 25

PRACTICE PROGRAMS

– Calculate area of rectangle, circle, or cylinder.
– Convert temperature (Fahrenheit to Celsius).
– Swap two numbers.
– Check divisibility by 2 using bitwise &.

COMMON ERRORS

– Using string input for arithmetic without conversion.
– Incorrect variable names or assignments.
– Syntax errors in print statements or string quotes.

QUICK QUESTIONS

– Difference between keywords and identifiers.
– Types of literals and their examples.
– Size of strings with escape sequences.
– Valid vs. invalid string declarations.
– Fixing input/output errors in code.

Leave a Reply

Your email address will not be published. Required fields are marked *