It is currently 27 Feb 2021, 12:11

• 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

Numbers in Python

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

Numbers in Python

Post#1 by tim » 08 Oct 2018, 14:59


Numbers are fun and easy in Python.
Numbers allow you to do math with them.

To declare a variable that is a number (named x) and set to initially as 10 you can do for example:
x = 10
or you can do some math and assign the result to a variable like this:
x = 2+2

You can guarantee order of operations by using brackets like this.
y = (2+2) * 3

For exponents, you can use the double star(*, also multiplication sign) like if you wanted to say 10 to the power of 2 (or 10 squared) you can write like below setting variable z to (10 to the power of 2).
z = 10**2

To convert any number to a string (in order to combine with other strings) you can use the str() function because 10 (the number 10) is not the same as "10" (the string containing "10"). Like for example you wanted to print the string "This is my number: " in front of a variable named x containing the number 10, you can write:
print("This is my number" + str(x))

You can play with what you've learned in this lesson here

Next lesson: Lists in Python Previous lesson: Variables in Python
aka Tin

         

Krikor
Forum Member

Post#2 by Krikor » 08 Nov 2018, 23:43


Tim,
  • How to concatenate Int + String ?
  • What is the opposite of str(x)? To make a string a number?
ok= "right"
print (variavel + " "+ ok)
x=10 + 2
print (x)
print (str(x))
y = 5
print (x+y)
d= str(y)
print (d)
print (ok + d)
print (x + d) --->> Line 18: TypeError: unsupported operand type(s) for Add: 'int' and 'str' :hammer_head: :tantrum: :gaah:

Thx Tim! :thumbsup:
"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 » 09 Nov 2018, 05:05


To convert from string "10" to 10 use int()
for example
my_var = "10"
print (int(my_var)+2)

You can't concatenate int + str that's why you have to use str(x) + your_string if you want to print x as a string.

I am so glad you asked that, as that means you have been playing with python :cool:
aka Tin

         

Krikor
Forum Member

Post#4 by Krikor » 11 Nov 2018, 12:09


tim wrote:
09 Nov 2018, 05:05
To convert from string "10" to 10 use int()
for example
my_var = "10"
print (int(my_var)+2)

You can't concatenate int + str that's why you have to use str(x) + your_string if you want to print x as a string.

I am so glad you asked that, as that means you have been playing with python :cool:
Yep, I am taking this opportunity to try to learn the Python language. I feel like a baby who can not even speak. But I'm trying, at least when I have some time.
Tim, many thanks, Tim for the lessons!
Spoiler:
a = 10
b = a**2
output_string = "My number squared is: " + str(b)
print (output_string)
print("-------------------------------------------")
home = "test1: " + str(a*b)
print (home)
double = "red" + "blue"
print (double)
print("-------------------------------------------")
c=5
d=20
print (c+d)
print("c =5 and d=20" +" "+"c+d= "+ str(c+d))
print("-------------------------------------------")
t= str(c)
p= str(d)
print("c=5 and d=20 But t=str(c) and p=str(d)")
print("")
print ("Print c: --> "+ str(c)+ " numeric")
print ("Print t: --> "+ str(t)+ " string")
print ("Print c+d")
print (c+d)
print ("Print t+p")
print (t+p)
"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#5 by tim » 11 Nov 2018, 15:05


Python is very intuitive, i find.
So you'll be running (instead of walking baby).
Keep practicing.

There's also a function called float() which converts string back to a real number (with decimals) (float). See more details here.
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