Development > Coding

int(3) and int('3') is there a difference? (python3.5)

(1/3) > >>

teage:
So I'm reading a python3 book and working with int(). I follow the directions everything is fine. They have me typing in the shell int('2') and see the result. I noticed if I type int(2) with no apostrophe i get the same result. So my question is this, is there a difference?

bitsnpcs:
Hello teage,
it can be used apostrophes where the integer is to print within a string using the speech marks " "

There is an example here  https://www.programiz.com/python-programming/methods/built-in/int

teage:

--- Quote from: bitsnpcs on February 05, 2019, 09:26:07 PM ---Hello teage,
it can be used apostrophes where the integer is to print within a string using the speech marks " "

There is an example here  https://www.programiz.com/python-programming/methods/built-in/int

--- End quote ---


So its a string that can be added, multiplied, divided etc?

example 1 of the url shows,
# integer
print("int(123) is:", int(123))

# float
print("int(123.23) is:", int(123.23))

# string
print("int('123') is:", int('123'))

so then I experiment with this,

>>> int('2')+int('2')
4

>>> int("2")+int("2")
4

>>> int(2)+int(2)
4

forgive me if my questions seem a bit silly, im just trying to wrap my head around it. :o

bitsnpcs:

--- Quote from: teage on February 05, 2019, 09:50:53 PM ---So its a string that can be added, multiplied, divided etc?

forgive me if my questions seem a bit silly, im just trying to wrap my head around it. :o


--- End quote ---


In this example yes.
Int can be used to convert a value into a number (an integer), then the calculations can be done on it etc.
The value can be one of any of the types.

What book are you using ?
It may be you are experimenting further than the current chapter covers, and things will be explained in more detail in future chapters.
You might need a faster paced book, or enjoy to make some projects extra to the book so you can experiment.

I am learning Python2, one book I use Python Crash Course by Eric Matthes I learn Python2 and Python3 along side, I am only on Chapter 3 of it, as I have been busy lately so only had time for 1 book to use for learning and that has been the Linux Command Line.
For when I complete my Python2 books, the first Python3 book I have and will use is called "O'Reilly Head First Python".

bitsnpcs:

 
Different ways to learn code, or learn most things.
Method one looks so boring. The branches dont really do much.
Method 2 looks alive, each dot is the outcomes of the chaos merging to form a result/project you do whilst learning , notice how the branches all go somewhere , they have a use, and a result. In reality most people will have huge numbers of strands on the image 2.
Method2 is good way to increase the pace of learning compared to Method1, the central line are the book/s you are learning from.


Navigation

[0] Message Index

[#] Next page

Go to full version