Notes Python Basics and features

0

Character Set

  • Letters: A-Z, a-z
  • Digits: 0-9
  • Special Symbols: space +-*/()~!@#$%^&[]{},;:”‘<.>/`
  • Whitespaces: Blank space, Enter, Tab
  • Other Characters: ASCII and Unicode characters

Tokens

  • Keywords: Reserved words with special meaning (e.g., if, else, while).
  • Identifiers: Names for variables, functions, classes, etc. (e.g., my_var, calculateTotal)
    • Rules: Must start with a letter or underscore, case-sensitive, no spaces or special characters except underscores.
  • Literals: Fixed values (e.g., 123, "hello")
    • Types: String, Numeric, Boolean, Special (None), Literal Collections
  • Operators: Symbols that perform operations (e.g., +, -, *, /)
  • Punctuators: Symbols used for program structure (e.g., (), {}, ,, ;)

Keywords

  • Reserved words with special meanings. Examples: def, return, import.

Identifiers

  • Forming Rules:
    • Can be a long sequence of letters and digits
    • Start with a letter or underscore
    • Case-sensitive
    • Cannot be a keyword
    • No special characters except underscore

Literals

  • String Literals: Enclosed in single or double quotes (e.g., "hello", '123')
  • Numeric Literals:
    • Integer: Decimal (e.g., 1234), Octal (e.g., 0o10), Hexadecimal (e.g., 0xF)
    • Floating Point: Decimal (e.g., 12.5), Exponent (e.g., 1.5e2)
  • Boolean Literals: True, False
  • Special Literal: None

Escape Characters

  • Special characters represented by a backslash (\):
    • \\ (Backslash)
    • \' (Single quote)
    • \" (Double quote)
    • \n (Newline)
    • \t (Tab)
    • \uXXXX (Unicode character)

String Types

  • Single Line: Ends on the same line (e.g., "Hello")
  • Multiline:
    • With Backslash: """Hello \ World"""
    • Triple Quotes: """Hello \n World"""

Numeric Literals

  • Integer: Can be decimal, octal (prefix 0o), or hexadecimal (prefix 0x).
  • Floating Point: Can be fractional or in exponential notation.

Type Conversion

  • Implicit Conversion: Done automatically by Python (e.g., int to float)
  • Explicit Conversion: Using functions like int(), float(), str(), bool()

Input and Output

  • Input: Use input() (returns string).
  • Conversion Needed: Convert input to appropriate type (int(), float()).
  • Output: Use print() with optional sep and end parameters to format output.

Operators

  • Unary: +, -, ~, not
  • Binary:
    • Arithmetic: +, -, *, /, %, **, //
    • Bitwise: &, |, ^, <<, >>
    • Relational: <, >, <=, >=, ==, !=
    • Logical: and, or, not
    • Assignment: =, +=, -=, *=, /=, //=, **=
    • Membership: in, not in
    • Identity: is, is not

Variables

  • Definition: Created by assignment (e.g., x = 10)
  • Dynamic Typing: Can change type (e.g., x = 100 then x = "text")

Expressions vs. Statements

  • Expression: Produces a value (e.g., 5 + 3)
  • Statement: Performs an action (e.g., print("Hello"))

Comments

  • Single Line: # comment
  • Multiline: """ comment """ or ''' comment '''

Functions

  • Definition: Use def keyword to define (e.g., def my_function():)

Blocks and Indentation

  • Block: Group of statements with consistent indentation.

Multiple Assignments

  • Single Value: a = b = c = 10
  • Multiple Values: x, y, z = 1, 2, 3

Common Errors

  • Type Mismatch: Ensure correct types when performing operations or conversions.

Practical Exercises

  • Basic Programs: Calculate area, convert units, process input, etc.

Special Cases and Cautions

  • Dynamic Typing Caution: Ensure correct type operations to avoid errors.

We will be happy to hear your thoughts

Leave a reply

Captcha

ScienceShala
Logo
Enable registration in settings - general