1

This is the sample code.

What is the type of fruit and letter in my code (whether int, list, dict, ...) when I have not declared any?

for letter in 'Python':    //**how letter is set to string** 
   print 'Current Letter :', letter

fruits = 1,45

for fruit in fruits:      /****/how fruit is set to int int**** 

   print 'Current fruit :', fruit

output

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : 1
Current fruit : 45
md venkatesh
  • 157
  • 9
  • 3
    You can use the [`type`](https://docs.python.org/3/library/functions.html#type) bultin function to get the type of any object. In your case, that would be `str` for the letters and `int` for the numbers. – tobias_k Mar 12 '17 at 09:26
  • before or after print use `type(letter)` and `type(fruit)` to see datatypes – Muhammad Haseeb Khan Mar 12 '17 at 09:28
  • 3
    Possible duplicate of [How to determine the variable type in Python?](http://stackoverflow.com/questions/402504/how-to-determine-the-variable-type-in-python) – McGrady Mar 12 '17 at 09:31
  • i have tried type(letter) it is showing no change in my output – md venkatesh Mar 12 '17 at 09:38
  • It should write as follows and . The first letters are string the fruit ones are int. – Turkdogan Tasdelen Mar 12 '17 at 09:54
  • You still need to print it, as in `print type(letter)`. `type` just returns the type, it can be used for other things. – Alex Hall Mar 12 '17 at 10:14

1 Answers1

0

You can use type() to know the type

for letter in 'Python':
      print 'Current Letter :', letter

fruits = 1,45

for fruit in fruits:

   print 'Current fruit :', fruit
print type(fruit)
print type(letter)

This will give the following output

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : 1
Current fruit : 45
<type 'int'>
<type 'str'>
George
  • 109
  • 8