CASTING, STRING, BOOLEAN

Casting in Python

CASTING, STRING, BOOLEAN IMAGE

Casting is a process of converting a variable's data type into another data type. If you want to specify the data type of a variable, this can be done with casting.

Example:

x = str(3)    # x will be '3'

y = int(3)    # y will be 3

z = float(3)  # z will be 3.0

Python String

Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes.

In Python, strings can be created by enclosing the character or the sequence of characters in the quotes. Python allows us to use single quotes, double quotes, or triple quotes to create the string.

Creating a string in Python

Syntax:

str = "Hi Python !"    

Here, if we check the type of the variable str using a Python script

print(type(str)), then it will print a string (str).    

In Python, strings are treated as the sequence of characters, which means that Python doesn't support the character data-type; instead, a single character written as 'p' is treated as the string of length 1.

#Using single quotes  

str1 = 'Hello Python'  

print(str1)

#Using double quotes  

str2 = "Hello Python"  

print(str2)

#Using triple quotes  

str3 = '''Triple quotes are generally used for represent the multiline or docstring'''   

print(str3)  

Output:

Hello Python

Hello Python

Triple quotes are generally used for represent the multiline or docstring

BOOLEAN

Booleans represent one of two values: True or False.

In programming you often need to know if an expression is True or False.

You can evaluate any expression in Python, and get one of two answers, True or False.

When you compare two values, the expression is evaluated and Python returns the Boolean answer:

print(10 > 9)

print(10 == 9)

print(10 < 9)

When you run a condition in an if statement, Python returns True or False:

Example:

Print a message based on whether the condition is True or False:

a = 200

b = 33

if b > a:

    print("b is greater than a")

else:

    print("b is not greater than a")

Evaluate Values and Variables

The bool() function allows you to evaluate any value, and give you True or False in return,

Example:

Evaluate a string and a number:

print(bool("Hello"))

print(bool(15))

Example:

Evaluate two variables:

x = "Hello"

y = 15

print(bool(x))

print(bool(y))

ALSO READ:


Total सच

एक टिप्पणी भेजें

और नया पुराने