CASTING



Python - Casting


Casting is when you convert a variable value from one type to another. This is, in Python, done with functions such as int() or float() or str().

Specify a Variable Type :

There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.

Casting in python is therefore done using constructor functions:


  • int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)

  • float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)

  • str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals


##Integers example :

x=int(1)
y=int(2.5)
z=int("6")
print(x)
print(y)
print(z)

===o/p===
1
2
6

## float example :

x=float(1)
y=float(25)
z=float("6")
print(x)
print(y)
print(z)

====o/p====
1.0
25.0
6.0

## strings example :

x=str("a")
y=str(23)
z=str("apple")
w=str(2.6)
print(x)
print(y)
print(z)
print(w)


===o/p===

a
23
apple
2.6


RegEx SETS

Python - RegEx SETS

posted on 2019-11-12 23:06:24 - Python Tutorials


RegEx_Functions

Python - RegEx_Functions

posted on 2019-11-09 06:07:29 - Python Tutorials


RegEx_Sets

Python - RegEx_Sets

posted on 2019-11-09 05:30:54 - Python Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials