Post

Basics of Python

Basics of Python

Variables

  • Variables are containers for storing the data values, with which we can further reference and manipulate the values.
  • Literals are values used in Python. Types are:
Data TypeValueDescription
NumericIntegerWhole numbers
 FloatDecimal number
 ComplexContains real and imaginary part
BooleanTrueRepresents the truth value
 FalseRepresents the false value
CollectionListOrdered and changeable collection
 TuplesOrdered and unchangeable collection
 DictionaryUnordered, changeable and indexed collection
 SetsUnordered and unindexed collection
StringSequence of CharactersA sequence of Unicode characters
SpecialNoneRepresents the absence of value
 Not a Number (NaN)Represents a numeric value that is undefined or unrepresentable

Operators in Python

Operator TypeOperatorDescription
Arithmetic OperatorsAddition: +Adds values on either side of the operator
 Subtraction: -Subtracts right hand operand from left hand operand
 Multiplication: *Multiplies values on either side of the operator
 Division: /Divides left hand operand by right hand operand
 Modulus: %Divides left hand operand by right hand operand and returns the remainder
 Exponentiation: **Performs exponential (power) calculation on operators
 Floor Division: //The division of operands where the result is the quotient in which the digits after the decimal point are removed
Comparison OperatorsEqual to: ==Returns True if both operands are equal
 Not equal to: !=Returns True if operands are not equal
 Greater than: >Returns True if left operand is greater than the right one
 Less than: <Returns True if left operand is less than the right one
 Greater than or equal to: >=Returns True if left operand is greater than or equal to the right one
 Less than or equal to: <=Returns True if left operand is less than or equal to the right one
Assignment OperatorsAssign: =Assigns values from right side operands to left side operand
 Add and assign: +=It adds right operand to the left operand and assign the result to left operand
 Subtract and assign: -=It subtracts right operand from the left operand and assign the result to left operand
 Multiply and assign: *=It multiplies right operand with the left operand and assign the result to left operand
 Divide and assign: /=It divides left operand with the right operand and assign the result to left operand
 Modulus and assign: %=It takes modulus using two operands and assign the result to left operand
 Floor division and assign: //=It performs floor division on operators and assign value to the left operand
 Exponent and assign: **=Performs exponent calculation on operators and assign value to the left operand
Logical OperatorsLogical AND: andReturns True if both the operands are true
 Logical OR: orReturns True if either of the operands is true
 Logical NOT: notReturns True if operand is false (complements the operand)
Bitwise OperatorsBitwise AND: &Performs bitwise AND operation
 Bitwise OR: |Performs bitwise OR operation
 Bitwise NOT: ~Inverts all the bits
 Bitwise XOR: ^Performs bitwise exclusive OR operation
 Bitwise right shift: >>Shift right by pushing copies of the leftmost bit on the left, and let the rightmost bits fall off
 Bitwise left shift: <<Shift left by pushing zeros in from the right and let the leftmost bits fall off
Identity OperatorsisReturns True if both variables are the same object
 is notReturns True if both variables are not the same object
Membership OperatorsinReturns True if a sequence with the specified value is present in the object
 not inReturns True if a sequence with the specified value is not present in the object

Collection Object - Strings:

Common Operations:

  1. Concatenation
    1
    2
    3
    
     x = "Hello"
     y = " Python"
     print(x+y)
    
  2. Repetition
    1
    2
    
     x = "Hello "
     print(x*2)
    
  3. Membership Operators
    1
    2
    3
    4
    5
    
     x = "hello python"
     if "hello" in x:
         print(True)
     else:
         print(False)
    
  4. Length
    1
    2
    
     x = "hello python"
     print(len(x))
    
  5. Indexing
    1
    2
    3
    
     x = "hello python"
     print(x[0])
     print(x[6])
    
  6. Slicing
    1
    2
    
     x = "hello python"
     x[1:7]
    

Builtin Functions

FunctionDescription
strip()Returns a copy of the string with both leading and trailing characters removed
lower()Converts all uppercase characters in a string into lowercase characters and returns it
upper()Converts all lowercase characters in a string into uppercase characters and returns it
len()Returns the number of items in an object
type()Returns the type of an object
print()Prints the specified message to the screen
input()Reads a line from input, converts it to a string and returns it
int()Converts a specified value into an integer
str()Converts a specified value into a string
float()Converts a specified value into a floating point number
list()Converts a specified value into a list
dict()Converts a specified value into a dictionary
set()Converts a specified value into a set
sorted()Returns a sorted list from the specified iterable
range()Returns a sequence of numbers, starting from 0 by default, and increments by 1 (also default), and stops before a specified number
sum()Returns the sum of all items in an iterable
max()Returns the item with the highest value in an iterable
min()Returns the item with the lowest value in an iterable

Data Types in Python

Python has several built-in data types that can be categorized as follows:

1. Numeric Types

Data TypeDescriptionExample
intInteger numbers (positive or negative whole numbers)x = 42, y = -10
floatFloating-point numbers (decimal numbers)pi = 3.14159, temp = -2.5
complexComplex numbers with real and imaginary partsz = 3 + 4j, real: 3, imag: 4

2. Boolean Type

Data TypeDescriptionExample
boolBoolean values representing truth valuesis_valid = True, has_error = False

3. Sequence Types

Data TypeDescriptionExample
listOrdered, mutable collection that allows duplicate membersfruits = ["apple", "banana", "cherry"]
tupleOrdered, immutable collection that allows duplicate memberspoint = (10, 20), colors = ("red", "green", "blue")
rangeSequence of numbers, typically used in loopsnumbers = range(5)[0, 1, 2, 3, 4]

4. Text Type

Data TypeDescriptionExample
strString of Unicode characters (text)name = "Python", message = 'Hello World'

5. Set Types

Data TypeDescriptionExample
setUnordered collection with no duplicate elementsvowels = {'a', 'e', 'i', 'o', 'u'}
frozensetImmutable version of a setconstants = frozenset([3.14, 2.71])

6. Mapping Type

Data TypeDescriptionExample
dictUnordered collection of key-value pairsperson = {"name": "John", "age": 30}

7. Binary Types

Data TypeDescriptionExample
bytesImmutable sequence of bytesdata = b'Hello'
bytearrayMutable sequence of bytesdata = bytearray(5)
memoryviewObject that exposes the memory buffer interfacemem = memoryview(bytes(5))

8. Special Type

Data TypeDescriptionExample
NoneTypeRepresents the absence of a value (null)result = None, empty_list = []

Quick Reference Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Numeric types
age = 25          # int
price = 19.99     # float
complex_num = 2+3j # complex

# Boolean
is_active = True

# Sequence types
items = [1, 2, 3]        # list
coordinates = (10, 20)   # tuple
numbers = range(5)       # range: 0,1,2,3,4

# Text
greeting = "Hello"

# Sets
unique_colors = {"red", "green", "blue"}

# Dictionary
student = {"name": "Alice", "grade": 85}

# Binary
data = b"test"
buffer = bytearray(10)

# None
result = None

List Comprehension

  • List comprehension is a concise and expressive way to create lists in python.
  • It provides a more compact syntax for creating lists by specifying the elements of the list and the conditions under which they should be included.
  • Sytax:
    • new_list = [expression for item in iterable if condition ]
  • Example:

    1
    2
    3
    4
    5
    6
    
      x = [1, 2, 3, 4, 5, 6, 7, 8]
    
      result = []
      for i in x:
          result.append(i**2)
      print(result)
    
  • Using List Comprehension
    1
    2
    3
    
      x = [1, 2, 3, 4, 5, 6, 7, 8]
      result = [i**2 for i in x]
      print(result)
    

    OR

    1
    2
    3
    
      x = [1, 2, 3, 4, 5, 6, 7, 8]
      result = [i**2 for i in x if i%2==0]
      print(result)
    

Buit-in Functions - Lists

FunctionDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

Buit-in Functions - Tuples

FunctionDescription
count()Returns the number of times a specified value occurs in a tuple
index()Searches the tuple for a specified value and returns the position of where it was found

Buit-in Functions - Sets

FunctionDescription
add()Adds an element to the set
clear()Removes all the elements from the set
copy()Returns a copy of the set
difference()Returns a set containing the difference between two or more sets
difference_update()Removes the items in this set that are also included in another, specified set
discard()Remove the specified item
intersection()Returns a set, that is the intersection of two other sets
intersection_update()Removes the items in this set that are not present in other, specified set(s)
isdisjoint()Returns whether two sets have a intersection or not
issubset()Returns whether another set contains this set or not
issuperset()Returns whether this set contains another set or not
pop()Removes an element from the set
remove()Removes the specified element
symmetric_difference()Returns a set with the symmetric differences of two sets
symmetric_difference_update()inserts the symmetric differences from this set and another
union()Return a set containing the union of sets
update()Update the set with the union of this set and others

Buit-in Functions - Dictionary

FunctionDescription
clear()Removes all the elements from the dictionary
copy()Returns a copy of the dictionary
fromkeys()Returns a dictionary with the specified keys and value
get()Returns the value of the specified key
items()Returns a list containing a tuple for each key value pair
keys()Returns a list containing the dictionary’s keys
pop()Removes the element with the specified key
popitem()Removes the last inserted key-value pair
setdefault()Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update()Updates the dictionary with the specified key-value pairs
values()Returns a list of all the values in the dictionary
This post is licensed under CC BY 4.0 by the author.