Python Complete Concepts with Samples

Mohamed Alikhan
5 min readMay 2, 2024
Let's Learn Programming

Let us discuss the complete Python language concepts and sample programs. Python is a high-level, interpreted, dynamically typed programming language known for its simplicity, readability, and versatility.

Base Types:

  1. int (Integer):
  • Represents whole numbers without any decimal point.
  • Examples: 0, -10, 42, 1000, etc.
  • It can represent positive, negative, and zero values.

2. float (Floating-point Number):

  • Represents real numbers with a decimal point.
  • Examples: 3.14, -0.5, 2.0, 1e-3 (scientific notation), etc.
  • It can represent fractional values and numbers in exponential form.

3. bool (Boolean):

  • Represents Boolean values True and False.
  • Used for logical operations and conditional expressions.
  • Often, the result of comparison operations or logical operations.
  • Example: True, False.

4. str (String):

  • Represents sequences of Unicode characters enclosed within single quotes (') or double quotes (").
  • Examples: 'hello', "Python", '123', "apple", etc.
  • It can contain letters, digits, symbols, and spaces.

5. bytes:

  • Represents sequences of bytes, typically used for binary data or raw data.
  • Encoded as a sequence of integers in the range 0 to 255.
  • Examples: b'hello', b'\x48\x65\x6c\x6c\x6f'.
  • Used for working with binary files, network protocols, and other low-level operations.

Container Types:

  1. list:
  • Ordered collection of items, mutable (can be modified after creation).
  • Elements are enclosed within square brackets [] and separated by commas.
  • Examples: [1, 2, 3, 4], ['apple', 'banana', 'cherry'], [True, False, True].
  • Allows duplicate elements and supports indexing and slicing.

2. tuple:

  • Ordered collection of items, immutable (cannot be modified after creation).
  • Elements are enclosed within parentheses () and separated by commas.
  • Examples: (1, 2, 3, 4), ('apple', 'banana', 'cherry'), (True, False, True).
  • It allows duplicate elements and supports indexing and slicing but cannot be modified once created.

3. dict (dictionary):

  • Unordered collection of key-value pairs, mutable.
  • Elements are enclosed within curly braces {} with keys and values separated by colons :.
  • Examples: {'name': 'John', 'age': 30, 'city': 'New York'}, {1: 'apple', 2: 'banana', 3: 'cherry'}.
  • Keys must be unique, values can be duplicated, and key-based access must be supported.

4. set:

  • An unordered collection of unique elements, mutable.
  • Elements are enclosed within curly braces {}.
  • Examples: {1, 2, 3, 4}, {'apple', 'banana', 'cherry'}.
  • Automatically removes duplicate elements and supports set operations like union, intersection, difference, etc.

Conversions:

Here, I will explain the allowed conversions in Python

int("15") # Converts string 15 to Integer
int("3f", 16) # Converts Hexadecimal to Integer. Output is 63
int(15.56) # Output 15. Truncate decimal part
float("-11.24e8") # Output -1124000000.0
round(15.56,1) # Output 15.6 Here it round of 1 decimal place
bool(x) # False for null x, empty container x, None or False x; True for other x
str(x) # representation string of x for display
chr(64) # Output @. Convert code to character
ord('@') # Output 64. Convert character to code
list("abc") # Output ['a','b','c']. Converts string characters to list elements
dict([(1,"one"),(2,"two")]) # Output {1:'one', 2:'two'}
set(["one","two"]) # Output {'one','two'}

Other conversions are

a) Assembled str -> Separator str and sequence of str

‘:’.join([‘name’ , ’address’ , ’phone’]) -> ‘name:address:phone’

b) str splitter on whitespaces or based on separator-> list of str

eg1: (“words with spaces”).split() -> [‘words’, ‘with’, ‘spaces’]

eg2: (“1,2,3,4”).split(“,”) -> [‘1’, ‘2’, ‘3’, ‘4’]

c) list comprehension — Sequence of one type to list of another type

[int(x) for x in (‘1’, ‘2’, ‘3’)] -> [1,2,3]

Identifiers:

For variables, functions, modules, and classes, uses the naming format below

a…zA….z_ followed by a…zA….Z_0…9

good variable formats are: a, toto, x7, y_max, bigOne

bad variable formats are: 8y, and, for etc

Variable Assignment:

binding a name with a value is called an assignment.

  1. evaluation of right side expression as the value
  2. assignment in order with the left side as names

eg:

a) x = 1.2 + 8 (Here, summation of 1.2 and 8 will be stored to variable x)

b) a = b = c = 0 (Here, a,b, and c are assigned by the same value 0)

c) x, y, z = 3, 5, 8 (This is multiple assignments. Each variable assigns the value sequentially)

d) a, b = b, a (This is for values swap)

e) x+=3 (same as x=x+3)

f) x-=2 (same as x=x-2)

x=None (x defined as undefined)

del x (remove name x)

Sequence Containers Indexing:

This concept is very important for list split operations. The majority of the interview questions related to the list are to be solved once we know the sequence indexing.

List sequence indexing

The syntax for accessing subsequence is lst[start slice : end slice : step] => Included Start slice index and excluded end slice index

Samples:

  1. len(lst) → 5 (length of list will return)
  2. lst[0] → 10 (index 0 value will return)
  3. lst[1] → 20 (index 1 value will return)
  4. lst[-1] → 50 (last value will return)
  5. lst[-2] → 40 (second last value will return)
  6. lst[ : -1] → [10, 20, 30, 40] (Here it takes everything from the start except the last element 50 since the end slice is -1)
  7. lst[1 : -1] → [20,30,40] (starts from 1st index and end by -1 index)
  8. lst[ : : 2] → [10, 30, 50] (step value 2, so from the start, it steps +2 index)
  9. lst[ : : -1] → [50, 40, 30, 20, 10] (This is one way to reverse the list)
  10. lst[ : : -2] → [50, 30, 10] (reverse list and skips in between values)
  11. lst[ : ] → [10, 20, 30, 40, 50] (Since we haven’t mentioned the start and end, it will consider all elements. This is a shallow copy of the sequence)
  12. lst[1 : 3] → [20, 30] (starts from index 1 till index 2)
  13. lst[-3 : -1] → [30, 40] (starts from -3 slice( index 2) till -1 slice (index 4))
  14. lst[: 3] → [10, 20, 30] (consider from start till index 3)
  15. lst[3 : ] → [40, 50] (consider from index 3 till end)

Start practicing multiple sequence lists to get an idea of sequence indexing

WORKING ON MORE CONTENT TO ADD…

--

--

Mohamed Alikhan
0 Followers

Loves coding and sharing knowledge to others.