Python

Cards (542)

  • Variables in Python:
    • No declaration is required for variables
    • Variables must be initialized before use
  • When an expression changes, Python associates a new object to the variable for referencing the value
  • Variables can hold objects of different types as required
  • Deleting variables:
    • Variables can be deleted using the del statement
  • Data Types in Python:
    • Numbers:
    • Integers have no limit for value range
    • Integers can be represented in binary, octal, and hexadecimal systems
  • Numbers:
    • Floating Point:
    • Float type designates floating-point numbers
    • Float values are represented with a decimal point
  • Numbers:
    • Floating Point:
    • Scientific Notation:
    • The character e or E followed by a positive or negative integer can be used for scientific notation
  • Numbers:
    • Complex Numbers:
    • Complex numbers are specified as <real part>+<imaginary part>j
  • Boolean:
    • Python provides a Boolean data type with values True or False
  • Strings in Python:
    • Strings can be represented in single (' ') or double (" ") quotes
    • Strings can contain as many characters as needed
  • type() function:
    • Returns the class type of the argument passed
    • Mostly used for debugging purposes
  • Arithmetic Operators:
    • Include addition, subtraction, multiplication, division, etc.
  • Relational Operators:
    • Used for comparison, such as equal to, not equal to, greater than, etc.
  • Logical Operators:
    • Include AND, OR, and NOT operators
  • Bitwise Operators:
    • Used for manipulation of individual bits
  • Identity Operators:
    • Compare memory locations of Python objects/variables
  • Membership Operators:
    • Test if a value is a member of other Python objects like strings, lists, or tuples
  • Type Conversion (Casting):
    • Implicit Type Casting
    • Explicit Type Casting
  • References:
    • Various resources for further learning and understanding Python data types and operators
  • Python Control Flow:
    • Apply different types of if statements in Python programs
    • Apply switcher statements in Python programs
    • Apply while and for loops in Python programs
    • Use range() function in Python programs
    • Use pass statement
  • Conditional Statements:
    Simple if statement:
    • Used when checking only one condition
    • Syntax: if Expression:
    • Example:
    age=int(input("Enter the age:"))
    if age >= 60:
    print("You are a senior citizen")
    print("End of script")
  • If...else statement:
    • Used when checking exactly 2 different conditions
    • Syntax: if Expression:
    • Example:
    age=int(input("Enter the age:"))
    if age >= 60:
    print("You are a senior citizen")
    else:
    print("You are not a senior citizen")
    print("End of script")
  • If...elif...else statement:
    • Used when checking more than 2 conditions
    • Syntax: if Expression_1:
    • Example:
    age=int(input("Enter the age:"))
    if age<13:
    print("You are a child.")
    elif age>=13 and age<18:
    print("You are a teenager.")
    elif age>=18 and age<60:
    print("You are an adult.")
    else:
    print("You are a senior citizen.")
    print("End of script")
  • Switcher statement:
    • The switch...case is not available in Python
    • Define a function to provide switcher
    • Example:
    def week(i):
    switcher={
    0:'Sunday',
    1. 'Monday',
    ...
    1. 'Saturday'
    }
    return switcher.get(i,"Invalid day of week")
    print(week(7))
    Output: Invalid day of week
  • Python Loops:
    While loop:
    • Syntax: while (checking condition):
    • Example:
    sum = 0
    i=1
    while i <= n:
    sum=sum+i
    i += 1
    print("The sum of first",n,"terms=",sum)
  • While...else loop:
    • Example:
    i = 1
    while i < 7:
    print(i)
    i += 1
    else:
    print("i is no longer less than 7")
  • Range() function:
    • Returns a sequence of numbers starting from 0 by default
    • Syntax: range(start, end, increment)
    • Example:
    for i in range(1,6):
    for j in range(1,i+1):
    print("@",end=' ')
    print()
  • A string in Python is a group of characters enclosed within single, double, or triple quotes
  • Strings are a sequential collection of characters
  • To create a string, the most commonly used syntax is double quotes
  • Triple single or triple-double quotes are used when creating multiple lines of a string
  • Inside a string, single and double quotes can be used
  • String indexing in Python can be done using positive and negative indexing
  • Positive indexing starts from 0 (zero) in the left to right direction
  • Negative indexing starts from -1 (minus one) in the right to left direction
  • String slicing in Python is used to access parts of a string
  • Slicing indices should be integers or None, otherwise errors will occur
  • The default value for the step in slicing is 1
  • Strings in Python are immutable, meaning their state cannot be changed once created
  • The + operator in Python is used for string concatenation