Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet42/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   38   39   40   41   42   43   44   45   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Storing User Input


In the previous cell, we simply printed out the input that the user put in. However, in order to work with the data that they enter, we need to store it into a variable:

# saving what the user inputs ans = input("What is your name? ") print("Hello { }!".format(ans) )

Go ahead and run that cell. Storing the information that the user puts in our program is as easy as storing it into a variable. This way we can work with the data they input at any point.
What Is Type Converting?
Python defines type conversion functions to directly convert one data type to another which is useful in day-to-day and competitive programming. In some situations, the data you’re working with may not be the correct type. The most obvious example is user input because no matter what the user types in, the input is taken as a string. If you are expecting a number to be input, you’ll need to convert the input to an integer data type, so that you’re able to work with it.

Checking the Type


Before we go over how to type convert, I’d like to touch on an important function that Python has which allows us to check the type of any given variable:

# how to check the data type of a variable num = 5 print( type(num) )

Go ahead and run that cell. The output here will be “”. Don’t worry about the class portion here, we’ll get into classes another week. Focus on the second part where it outputs the type as an integer. This allows us to check what data type where working with.

Converting Data Types


Python gives us the ability to type convert easily from one type to another simply by wrapping the type around the variable. Let’s check out an example of converting a string to an int:

# converting a variable from one data type to another num = "9" num = int(num) # re-declaring num to store an integer print( type(num) ) # checking type to make sure conversion worked

ChApTer 3 User InpUT And CondITIonAls
Go ahead and run that cell. We’ve just converted the string of “9” to an integer. Now we can use the variable num in any calculations. For the conversion to process correctly, we used the int() type conversion. Whatever data type is put inside of the parenthesis is converted into an int. Check Table 3-1 for how to convert from one data type to another.
Table 3-1. Converting data types


Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   38   39   40   41   42   43   44   45   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish