Ternary Operator in Python

This is the short hand name of if - else. It is also know as conditional operator. Its allow to test the condition in single line.

Syntax :-

[on_true] if [expression] else [on_false]

Example :-

a,b = 7,5
print(a) if a>b else print(b)
max = a if a>b else b
print(max)
Python

Output :-

7
5
Markup

I hope you understand the concept of ternary operator in python. If you have any suggestion or query please do a comment.

Thank You.

Type Conversion and Type Function()

Type Conversion :-

Type conversion means converting one data type into another data. Ex – Converting integer value into string or converting string into integer value

Type of Conversion :-

  1. Implicit type conversion :- Those type conversion are done by compiler or interpreter by it self is called Implicit type conversion
  2. Explicit Type conversion :- Those type conversion are done my user is called Explicit type conversion.

Example #1:- Implicit Type Conversion

1
2
3
4
5
6
7
8
9
10
11
x = 10
  
print("x is of type:",type(x))
  
y = 10.6
print("y is of type:",type(y))
  
x = x + y
  
print(x)
print("x is of type:",type(x))

Output :-

x is of type: <class 'int'>

y is of type: <class 'float'>

20.6

x is of type: <class 'float'>

Explanation :- Here we add one integer and one float type value after adding those both value in converted into float this conversion is done by compiler or interpreter is called implicit type conversion.

Example #2 :- Explicit type conversion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# converting int into string
a = 31
print(str(a))
 
# converting string into int
c = "5"
print(int(c))
 
# converting float into int
b = 4.23
print(int(b))
 
# converting int into float
e = 2
print(float(e))

Output :-

31                                                                                                                                            
                                                                                                                               
5                                                                                                                                             

4                                                                                                                                             

2.0  

Type () Function :-

type () function is used to check the data type of any variable

Example #3 :- Checking the data type of variables

1
2
3
4
5
6
7
8
a = 322
print(type(a))
 
b = "ABC"
print(type(b))
 
c = 23.2
print(type(c))

Output :-

<class 'int'>                                                                                                                                 

<class 'str'>  
                                                                                                                               
<class 'float'>

I hope you understand the concept of type conversion and type function in python. If you have any query or suggestion please do a comment.

Thank You.

 

Featured Post

List in Python

  List are just like dynamic array, declared in other languages( like vector in c++ and array list in java ). A single list may contain data...