views:

110

answers:

3

Hi, 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 posible 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 shorttest way to do this? P.S. i know you dont 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'.

+11  A: 

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
Ignacio Vazquez-Abrams
+2  A: 

Python isn't necessarily easier/faster than C, though it's possible that it's simpler ;)

To clarify another statement you made, "you don't have to declare the data type" - it should be restated that you can't declare the data type. When you assign a value to a variable, the type of the value becomes the type of the variable. It's a subtle difference, but different nonetheless.

KevinDTimm
I would say that C is simpler than python. It lacks decorators, metaclasses, descriptors, etc. Granted, those can all be implemented in C but then it's your program that's sophisticated and not the underlaying language.
aaronasterling
@aaron - note I said possible :) I've done C for more than 25 years and python for only a year - I still write all my little stuff in C (but not because it's simple)
KevinDTimm
The language might be simpler in terms of functions. But the point is it's NOT simpler to implement decorators, metaclasses, descriptors, etc
Falmarri
A: 

Everything in Python is an object, and that includes classes, class instances, code in functions, libraries of functions called modules, as well as data values like integers floating-point number, and strings, or containers like lists and dictionaries. It even includes namespaces which are dictionary-like (or mapping) containers which are used to keep track of the associations between identifier names (character string objects) and to the objects which currently exist. An object can even have multiple names if two or more identifiers become associated with the same object.

Associating an identifier with an object is called "binding a name to the object". That's the closest thing to a variable declaration there is in Python. Names can be associated with different objects at different times, so it makes no sense to declare what type of data you're going to attach one to -- you just do it. Often it's done in one line or block of code which specifies both the name and a definition of the object's value causing it to be created, like <variable> = 0 or a function starting with a def <funcname>.

How this helps.

martineau