Python: data types and the input() function


There are several data types in the Python programming language, for example:

  • integers int(x), for example, 1-2-3-4;
  • fractional numbers float(x), for example, 1.0-2.0-3.5;
  • str(x) strings, which can contain text and other characters.

The input() function has already been discussed earlier, this function allows the user to enter data into the program, this data can be further used.

Usage example:
name=input(‘What is your name?\n’)
print(‘Hello,’,name)

Essentially, the input() statement does two things:

  • First, it shows the user the string that was written inside the brackets.
  • After that, it allows the user to enter text, and after the user presses Enter, it ends its action.

The user can enter both text and numbers. By default, input() returns a string. That is, if you want to get a string from the user, then the input () function can be used by itself. If you want to get numeric values, then you need to set the data type for the function:

  • int(input()) – for integer values;
  • float(input()) – for fractional values.

This feature of the input() function is illustrated by the Cat’s Expenses program:

print(”’Cat’s expenses

The program calculates the amount of expenses in rubles”’)
korm=int(input(‘Feed: ‘))
toys=int(input(‘Toys: ‘))
shleyka=int(input(‘Shleyka: ‘))
rashodi=korm+toys+shleyka
print(‘\nCat’s expenses are:’, rashodi)
input(‘\nPress entr to exit’)

Conspect of the book: Майкл Доусон “Программируем на Python”, 2014 (Michael Dawson “Python Programming for the Absolute Beginner”, 3rd Edition).


This entry was posted in Python (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.