Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to declare variable type, C style in python

I'm a programming student and my teacher is starting with C to teach us the programming paradigms, he said it's ok if I deliver my homework in python (it's easier and faster for the homeworks). And I would like to have my code to be as close as possible as in plain C.
Question is:
How do I declare data types for variables in python like you do in C. ex:

int X,Y,Z; 

I know I can do this in python:

x = 0 y = 0 z = 0 

But that seems a lot of work and it misses the point of python being easier/faster than C. So, whats the shortest way to do this?
P.S. I know you don't have to declare the data type in python most of the time, but still I would like to do it so my code looks as much possible like classmates'.

like image 498
Guillermo Siliceo Trueba Avatar asked Oct 14 '10 12:10

Guillermo Siliceo Trueba


People also ask

Can I declare variable type in Python?

Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

How do you assign a datatype to a variable in Python?

Specify a Variable Type Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)

How do you declare a variable explicitly in Python?

There is no binding in Python to declare variables before we use it. We also need not explicitly declare variables with their data type. As soon as we assign a value to Python variables, they get declared automatically. We make use of the “=” operator for value assignment.


2 Answers

Starting with Python 3.6, you can declare types of variables and funtions, like this :

explicit_number: type 

or for a function

def function(explicit_number: type) -> type:     pass 

This example from this post: How to Use Static Type Checking in Python 3.6 is more explicit

from typing import Dict  def get_first_name(full_name: str) -> str:     return full_name.split(" ")[0]  fallback_name: Dict[str, str] = {     "first_name": "UserFirstName",     "last_name": "UserLastName" }  raw_name: str = input("Please enter your name: ") first_name: str = get_first_name(raw_name)  # If the user didn't type anything in, use the fallback name if not first_name:     first_name = get_first_name(fallback_name)  print(f"Hi, {first_name}!") 

See the docs for the typing module

like image 179
Cam T Avatar answered Oct 05 '22 18:10

Cam T


Edit: Python 3.5 introduced type hints which introduced a way to specify the type of a variable. This answer was written before this feature became available.

There is no way to declare variables in Python, since neither "declaration" nor "variables" in the C sense exist. This will bind the three names to the same object:

x = y = z = 0 
like image 41
Ignacio Vazquez-Abrams Avatar answered Oct 05 '22 18:10

Ignacio Vazquez-Abrams