It is currently 27 Feb 2021, 12:12

• Featuring Members' Works (Image Attachments) •
1: member
2: member
3: member
4: member
5: member

GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  🡆 Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

If...Else... in Python

Simple lessons to get your started in Python language
tim
GL Blogger

If...Else... in Python

Post#1 by tim » 09 Nov 2018, 17:49


This part should prove to be more fun for you because we'll deal with controlling logics.

In Python, you can use If Else statements to control logics, for example if you have a variable x (10):
x = 10

and you wanted to print out something depending if x is equals to 10 or not you can use

Code: Select all

if (x==10):
    print ("x is ten!")
else:
    print ("x is NOT ten!")
Take note of the colons at end of if statement and else statement (indicating beginning of a block of code).
You don't have to have the else statement and its block of code at all. It's just there to show you the syntax if you need to use the else.

Notice to compare if value is equal to some other value we use double equal sign (==). This is so because the single equal sign (=) indicates variable assignment and so we don't confuse it between trying to compare x to value and setting x to hold a value.

You can have an elif (elseif statement) statement between the if and else statements if you want to check for additional conditions before running the else statement.
For example you can do this

Code: Select all

x = 8
if (x==10):
    print ("x is ten!")
elif (x==8):
    print ("x is eight!")
elif (x==6):
    print ("x is six!")
else:
    print ("x is NEITHER ten, eight NOR six!")
You can play with what you've learned here.

Next lesson: Functions/Methods in Python Previous lesson: Ranges and For Loops in Python
aka Tin

         

Krikor
Forum Member

Post#2 by Krikor » 11 Nov 2018, 16:36


tim wrote:
09 Nov 2018, 17:49
In Python, you can use If Else statements to control logics, for example if you have a variable x (10):
x = 10

You can play with what you've learned here.
Tim, I did not see a relationship between this topic (If else ...) and the material presented in CodeSkulptor3 suggested by the link. :?

Nothing about If Else there ... :shock:

Code: Select all

def add_ten(num):
    return_value = num + 10
    return return_value

print (add_ten(5))
print (add_ten(2))

my_func = add_ten
print (my_func(10))[code]
"I feel that in both art and music, it's not the success that matters but the pleasure it gives you. Focus on the pleasure and the learning will come naturally." - Brian Weston

tim
GL Blogger

Post#3 by tim » 11 Nov 2018, 17:08


Cut and paste mistake.
Thanks for finding this.
Check it now.
aka Tin

         


GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  🡆 Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Post Reply New Topic
  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest