SoFunction
Updated on 2024-11-07

Python Beginner's Logic and if Statements and Usage in a Nutshell

Logical Judgments and Logical Statements

  • Whether something is right or wrong (a judgment of truth or falsehood) √ X
  • Doing different things based on the results of the judgment is our logical business
  • A judgment statement for which a condition is satisfied is a conditional statement
  • A logical statement is a combination of a conditional statement and a business statement

在这里插入图片描述 

If Statement Functions

Determine the truth of a proposition, if the proposition is true ( True ) then execute the if logic statement

If Statement Usage

Usage.

if bool_result: # Syntax blocks
	do # Business Code Blocks Note Indentation

Parameters.

  •  bool_result:: Determine the truth or falsity of the result, Boolean type
  • do: execute any python code if bool_result is True

Return Value: if is a keyword and has no return value.

>>> insane_status = 'hunger
>>> if insane_status == 'hunger':
		print( 'xiaomu invites Insane to dinner' )
		
xiaomu invites Insane to dinner

real combat

# coding:utf-8

info = 'my name is insane'

info_list = ()
print(info_list)

if info_list[0] == 'insane':
    print(1)
    info_list[0] = 'loafer'

if info_list[1] == 'insane':
    print(2)
    info_list[1] = 'loafer'

if info_list[2] == 'insane':
    print(3)
    info_list[2] = 'loafer'

if info_list[-1] == 'insane':
    print(4)
    info_list[-1] = 'loafer'

print(info_list)
info = ' '.join(info_list)
print(info)

info = 'my name is insane, i am a pythoner, i love python'
info_list = ()

if info_list[0] in ['python', 'i']:
    info_list[0] = '*'

if info_list[1] == 'python' or info_list[1] == 'i':
    info_list[1] = '*'

if info_list[2] == 'python' or info_list[2] == 'i':
    info_list[2] = '*'

if info_list[3] == 'python' or info_list[3] == 'i':
    info_list[3] = '*'

if info_list[4] == 'python' or info_list[4] == 'i':
    info_list[4] = '*'

if info_list[5] == 'python' or info_list[5] == 'i':
    info_list[5] = '*'

if info_list[6] == 'python' or info_list[6] == 'i':
    info_list[6] = '*'

if info_list[7] == 'python' or info_list[7] == 'i':
    info_list[7] = '*'

if info_list[8] == 'python' or info_list[8] == 'i':
    info_list[8] = '*'

if info_list[9] == 'python' or info_list[9] == 'i':
    info_list[9] = '*'

if info_list[-1] in ['python', 'i']:
    info_list[-1] = '*'

print(info_list)
info = ' '.join(info_list)
print(info)

info = 'my name is insane'
print(len(info))

if len(info) > 10 and len(info) != 15:
    print(('insane', 'loafer'))

if len(info) > 10 and len(info) != 17:
    print(('name', 'iop'))

print('finish')
['my', 'name', 'is', 'insane']
4
['my', 'name', 'is', 'loafer']
my name is loafer
['my', 'name', 'is', 'insane,', '*', 'am', 'a', 'pythoner,', '*', 'love', '*']
my name is insane, * am a pythoner, * love *
17
my name is loafer
finish

Process finished with exit code 0

To this article on Python beginner logic and if statement is introduced to this article, more related python logic if statement content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!