Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming
Python Projects for Beginners A Ten Week Bootcamp Approach to Python
Note w3 schools3 has great reference material for additional information on the many different types of operators. Checking User InputA great use for our newly learned conditional statement is for checking user input. Let’s try: # checking user input ans = int( input("What is 5 + 5? ") ) if ans == 10: print("You got it right!") Go ahead and run that cell. Our conditional statement checks to see if the user’s input is equal to the integer 10. If it is, then the indented print statement will run. Notice in line two that we ask for user input and immediately convert their answer to an integer. As we did not use a try and except, inputting a non-number would result in an error. Logical OperatorsLogical operators are used to combine conditional statements. You can write as many conditions on a single “if statement” as you’d like. Depending on the logical operators used, the if statement may or may not run due. Let’s look at the three logical operators we can use. Logical Operator “and”The “and” logical operator is to ensure that, when you check multiple conditions, both sides of the condition are True. This means that if either the condition to the left or right of the “and” is False, then the code will not run the block of code. Let’s try an example: # using the keyword 'and' in an 'if statement' x, y, z = 5, 10, 5 if x < y and x == z: print("Both statements were true") Go ahead and run that cell. The output will result in “Both statements were true” because x is less than y and the same value as z. Note You can have as many conditions in one line as you’d like. Logical Operator “or”The “or” logical operator is used to check for one or both conditions to be true. Such that if the condition to the left is False and the condition to the right is True, the block of code will still run because at least one condition was True. The only time an “if block” will not run using an “or” operator is when both conditions are False. Let’s check out an example: # using the keyword 'or' in an 'if statement' x, y, z = 5, 10, 5 if x < y or x != z: print("One or both statements were true") Go ahead and run that cell. Notice that we get an output of “One or both statements were true”. This worked even though our second condition is False, since x is equal to z and we we’re checking if it was not equal to it; however, since the condition on the left is True, it runs. Download 2,61 Mb. Do'stlaringiz bilan baham: 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 |