Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Comprehensive Python Basics Tutorial with Code Examples and Tables

Tech May 16 1

1. Absolute Value Calculation

abs(-6)  # Returns 6

2. All Elements Check

all([1,0,3,6])  # Returns False

3. Any Element Check

any([0,0,1])  # Returns True

4. ASCII Representation

class Person:
    def __init__(self, id, name):
        self.id = id
        self.name = name
    def __repr__(self):
        return f'id={self.id}, name={self.name}'

person = Person('001', 'Alice')
ascii(person)  # Returns 'id=001, name=Alice'

5. Decimal to Binary

bin(10)  # Returns '0b1010'

6. Decimal to Octal

oct(9)  # Returns '0o11'

7. Decimal to Hexadecimal

hex(15)  # Returns '0xf'

8. Truth Value Testing

bool([0,0,0])  # Returns True

9. String to Bytes

bytes('apple', 'utf-8')  # Returns b'apple'

10. Type Conversion to String

str(100)  # Returns '100'

11. Callable Object Check

class Calculator:
    def __call__(self):
        print("Performing calculation")

calc = Calculator()
callable(calc)  # Returns True

12. Decimal to ASCII Chaarcter

chr(65)  # Returns 'A'

13. ASCII to Decimal

ord('A')  # Returns 65

14. Class Method Definition

class MathUtils:
    @classmethod
    def square(cls, x):
        return x*x

15. String Code Execution

exec('print("Hello, World!")')

16. Complex Number Creation

complex(1, 2)  # Returns (1+2j)

17. Attribute Removal

delattr(person, 'id')

18. Dictionary Creation

dict(a='1', b='2')  # Returns {'a': '1', 'b': '2'}

19. Object Directory Inspection

dir(person)

20. Division with Quotient and Remainder

divmod(10, 3)  # Returns (3, 1)

21. Enumeration

for index, value in enumerate(['a','b','c'], 1):
    print(index, value)

22. Expression Evaluation

eval('1 + 3 + 5')  # Returns 9

23. Memory Size Calculation

import sys
sys.getsizeof({'x': 1, 'y': 2.5})

24. Filtering Elements

list(filter(lambda x: x > 10, [1, 11, 2, 45]))

25. Float Conversion

float(3)  # Returns 3.0

26. String Formatting

"Value: {0:.2f}".format(3.14159)  # Returns 'Value: 3.14'

27. Immutable Set Creation

frozenset({1, 2, 3})

28. Attribute Access

getattr(person, 'name')

29. Attribute Existnece Check

hasattr(person, 'age')

30. Hash Value Calculation

hash(person)

31. Help Documentation

help(person)

32. Object Identity

id(person)

33. User Input

input()

34. Integer Conversion

int('12', 16)  # Returns 18

35. Type Checking

isinstance(person, Person)

36. Inheritance Check

issubclass(ChildClass, ParentClass)

37. Iterator Creation

iter([1, 2, 3])

38. Base Object Class

object()

39. File Handling

open('file.txt', 'r')

40. Exponentiation

pow(2, 3, 5)  # Returns 3

41. Output Printing

print(f"Result: {result}")

42. Property Implementation

class Temperature:
    @property
    def celsius(self):
        return self._celsius

43. Range Generation

range(0, 10, 2)

44. Reverse Iteration

reversed([1, 2, 3])

45. Rounding

round(2.675, 2)  # Returns 2.67

46. Set Conversion

set([1, 1, 2, 3])  # Returns {1, 2, 3}

47. Slice Object

slice(0, 5, 2)

48. Sorting

sorted([3, 1, 2], reverse=True)

49. Summation

sum([1, 2, 3], 10)  # Returns 16

50. Tuple Conversion

tuple([1, 2, 3])

51. Type Inspection

type(person)

52. Zip Iteration

list(zip([1, 2, 3], ['a', 'b', 'c']))

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.