• Home
  • Jobs
  • coursesAndChallenges
  • Teachers
  • For business
  • Blog
  • ES/EN

0

40
Views
How to declare multiple variables with type annotation syntax in Python?

As far as I know, now we can declare variables using type annotation syntax in Python 3.6 as following code.

def printInt():
    a: int = 0
    b: int = 1
    c: int = 2
    print(a, b, c)

What I want to do is declaring variables a, b, c in one line.

I tried a, b, c: int, but it returns error.

Also a: int=0, b: int=1, c: int=2 returns error too.

Is there any way to declare multiple variables using type annotation syntax in one line?

about 1 month ago ·

Santiago Trujillo

3 answers
Answer question

0

If you really want to use annotation then you can do this in this form:-

a: int;b: int;c: int
a,b,c = range(3)
print(a,b,c) #As output 0 1 2
about 1 month ago · Santiago Trujillo Report

0

 a: int = 0; b: int = 1; c: int = 2

would actually work. If you are looking for a way to avoid repeating int all times, I am afraid you cannot as of Python 3.7.

about 1 month ago · Santiago Trujillo Report

0

Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.

a,b,c=0,1,2

Or the following maybe what you are looking for

def magic(a: str, b: str) -> int:
    light = a.find(b)
    return light
about 1 month ago · Santiago Trujillo Report
Answer question
Remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Startups
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.