Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python Intro class

Python Intro class

202f459dd0d7212eb5a0ab4928ee0af4?s=128

peterson oaikhenah

May 03, 2021
Tweet

Transcript

  1. None
  2. None
  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. None
  14. None
  15. None
  16. None
  17. None
  18. None
  19. None
  20. None
  21. None
  22. None
  23. None
  24. None
  25. None
  26. Explicit Type Conversion In Explicit Type Conversion, users convert the

    data type of an object to required data type. We use the predefined functions like int(), float(), str(), etc to perform explicit type conversion. This type of conversion is also called typecasting because the user casts (changes) the data type of the objects. Syntax : <required_datatype>(expression)
  27. Example : Addition of string and integer using explicit conversion

    num_int = 123 num_str = "456" print("Data type of num_int:",type(num_int)) print("Data type of num_str before Type Casting:",type(num_str)) num_str = int(num_str) print("Data type of num_str after Type Casting:",type(num_str)) num_sum = num_int + num_str print("Sum of num_int and num_str:",num_sum) print("Data type of the sum:",type(num_sum))
  28. When we run the above program, the output will be:

    In the above program, • We add num_str and num_int variable. • We converted num_str from string(higher) to integer(lower) type using int() function to perform the addition. • After converting num_str to an integer value, Python is able to add these two variables. • We got the num_sum value and data type to be an integer.
  29. Key Points to Remember 1. Type Conversion is the conversion

    of object from one data type to another data type. 2. Implicit Type Conversion is automatically performed by the Python interpreter. 3. Python avoids the loss of data in Implicit Type Conversion. 4. Explicit Type Conversion is also called Type Casting, the data types of objects are converted using predefined functions by the user. 5. In Type Casting, loss of data may occur as we enforce the object to a specific data type.
  30. None
  31. None
  32. None
  33. None
  34. None
  35. Control flow

  36. None
  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. None
  44. None
  45. None
  46. None
  47. None
  48. None
  49. None
  50. None
  51. None
  52. None
  53. None
  54. None
  55. None
  56. None
  57. None
  58. None
  59. None
  60. None
  61. None
  62. None
  63. None