Assigning Values in Python and Other Languages

python for data science

Here we will discuss how to assign values to variables in python and other languages for data science.

Method 1: Direct Initialization method

#Code to demonstrate variable assignment in Python 3

#Using Direct Initialisation Method

#initialising variable directly

a = 10

# So, to print value

print (“value of a is: ” + str(a))

Output:

value of a is: 10

Method 2: Using conditional operator

Below is a code in C++

// So, C++ code to demonstrate variable assignment

// upon condition using Conditional Operator

#include <bits/stdc++.h>

using namespace std;

int main()

{

// initialising variables using Conditional Operator

int a = 20 > 10 ? 1 : 0;

// printing value of a

cout<< “The value of a is: ” << a;

}

Output:

The value of a is: 1

One liner if-else in place of Conditional Operator in Python for Data Science

# So, code for demonstrating variable assignment

# by using One liner if-else

#initialising variable using Conditional Operator

# a = 30 > 20 ?1 : 0 is not possible in Python

#Instead there is one liner if-else

a = 1 if 30 > 20 else 0

# So, printing the value of a

print (“Value of a is: ” + str(a))

Output:

Value of a is: 1

So, to learn more, you can check out this or for python data science check this

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.