Introduction To Python Elaborate
Introduction To Python Elaborate
• Example:
• # This is a comment
• """This is a
• multi-line comment"""
Indentation in Python
• • Indentation is mandatory in Python.
• • Used to define blocks of code.
• • Standard: 4 spaces.
• Example:
• if x > 0:
• print("Positive")
Multi-Line Statements
• • Use backslash `\` to break long lines.
• • Or use parentheses `()`, brackets `[]`, or
braces `{}`.
• Example:
• total = (1 + 2 + 3 +
• 4 + 5 + 6)
Multiple Statement Group (Suite)
• • Group of statements controlled by a clause
(if, while, def).
• • Indented block after a colon `:` is the suite.
• Example:
• def greet():
• print("Hello")
• print("Welcome")
Quotes in Python
• • Python supports single (' '), double (" "), and
triple quotes (''' ''' or """ """).
• • Triple quotes allow multi-line strings.
• Examples:
• 'Hello'
• "World"
• '''Multi-line'''
Input, Output and Import
Functions
• • `input()` – get user input (as string)
• • `print()` – display output
• • `import` – include external modules
• Example:
• name = input("Enter name: ")
• print("Hello", name)
• import math
Operators in Python
• • Arithmetic: +, -, *, /, %, **, //
• • Comparison: ==, !=, >, <, >=, <=
• • Logical: and, or, not
• • Assignment: =, +=, -=, etc.
• • Bitwise and identity operators also available.
Data Types – Numbers
• • int – whole numbers (10, -5)
• • float – decimal numbers (3.14, -0.5)
• • complex – complex numbers (2 + 3j)
• Use type() to check data type.
Data Types – Strings
• • Sequence of characters inside quotes.
• • Immutable.
• • Support slicing, concatenation, repetition.
• Example:
• s = "Hello"
• print(s[0]) # H
Data Types – List
• • Ordered, mutable, allows duplicate
elements.
• • Defined with square brackets [].
• Example:
• fruits = ["apple", "banana", "cherry"]
Data Types – Tuple
• • Ordered, immutable, allows duplicate
elements.
• • Defined with parentheses ().
• Example:
• tuple1 = (1, 2, 3)
Data Types – Set
• • Unordered, mutable, no duplicates.
• • Defined with curly braces {}.
• Example:
• set1 = {1, 2, 3, 2} # {1, 2, 3}
Data Types – Dictionary
• • Key-value pairs.
• • Unordered (Python 3.6+ maintains insertion
order).
• Example:
• d = {"name": "Janani", "age": 20}
Data Type Conversion
• • Use functions like int(), float(), str(), list(),
tuple(), set(), dict().
• Examples:
• int("5") → 5
• str(5) → "5"
• list("abc") → ['a', 'b', 'c']