Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Assign Variable Inside an Expression in Python

  • 19 Nov, 2025
  • Com 0
Assigning a Value to a Variable Inside an Expression in Python

If you are using a Python 3.8+ version, you can use the Walrus Operator (:=) also known as the assignment operator, to assign a value to a variable inside an expression, like an if or while condition.

Syntax

if (var := expression):
    pass

Visual representation

Assigning a value inside an if condition in Python

Assigning a value inside an if condition

Let’s say we define a custom function and, inside the if condition, fetch its value and assign it to a new variable. If it has a value, we print that variable.

def fetch_data():
    # Simulate fetching data
    return {"key": "value"}


if (data := fetch_data()):
    print("Received:", data)
else:
    print("No data found")

# Output:
# Received: {'key': 'value'}

The fetch_data() function returns a dictionary with key/value pairs. So, that dictionary is the returned value, and we assign it to a variable data using the assignment operator (:=).

The data was received successfully, and we printed it to the console using the print() function.

Assigning in while conditions

Let’s say you are reading a file and assigning its contents to a variable; you can use the walrus operator (:=) in the while condition. This is the most fruitful way to use the walrus operator.

Here is the app.txt file that we will read:

app.txt file for reading

Let’s use the with open() context manager to open a file and use the readline() method to read a file line by line, and store its content in a variable.

with open("app.txt") as f:
    while (line := f.readline().strip()):
        print("Line:", line)

# Output:
# Line: Hello, World!

In this code, the variable line contains the first line of the app.txt file, “Hello, World!”.

Assigning inside Ternary (Conditional Expression)

In a conditional assignment, it chooses a value based on a condition and assigns it to a variable; all things will be done in one line. It does not create an if block; it produces a value.

input = int(s) if (s := input("Enter a number: ")) else 0

print("Value:", input)

# Output:
# Enter a number: 21
# Value: 21

In this code, the s := input() function assigns the value entered from the console to the variable “s”.

Here, the condition checks if s is non-empty (truthy).

If s is non-empty, the int(s) function is assigned to the input. If s is empty, 0 is assigned to the input. 

Finally, we are printing the input’s value.

That’s all!

Post Views: 6
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Check If Numpy Array Contains an Element
How to Print a Numpy Array in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend