0

How we can change data type in python.

11th Sep 2021, 7:12 PM
Bilal Hamza
Bilal Hamza - avatar
25 odpowiedzi
+ 5
By writing the type you wish before that specific type we got: For example changing the type float to string: X = 3.4 ---> type(X) ---> <class float> str(X) ---> type(X) ----> <class str> And so on...
13th Sep 2021, 3:11 PM
Amirreza
Amirreza - avatar
+ 5
Calvin T individuals starting out do not know about typecasting. there is a difference in asking how to chane a str to int and how to change data types. i dont think its a duplicate. its like saying a tuple and a list are the same thing.
12th Sep 2021, 3:56 AM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 3
AJ not really, that shows casting a string to an int. Bilal, you can change the data in a number of ways. You can cast the data, like AJ’s link, using the primitive datatypes method int(), float(), tuple(), list() etc. Be aware that this doesn’t modify the original. For Pandas/Numpy objects you can use the astype() method. Converting from some datatypes require more work, for example converting lists into dictionaries. With this you would need to iterate over each item. Other oddities may also need to be considered, like precision with int to float. Can use floor/ceiling functions to improve. For example... import math n1 = 1.99999999999 n2 = 1.9999999999999999 print(int(n1), int(n2)) (Outputs: 1 2) print(math.ceil(n1), math.ceil(n2)) (Outputs: 2 2) So it depends on what you are converting and how accurate you want the results.
11th Sep 2021, 7:27 PM
DavX
DavX - avatar
+ 3
AJ What I was saying is the question is vague, there are different ways to change data types for different types. Each have their own merit and that you can’t apply the same string -> int method for all types.
11th Sep 2021, 7:47 PM
DavX
DavX - avatar
+ 3
DavX Ok boss understood what you want to say.
11th Sep 2021, 7:56 PM
A͢J
A͢J - avatar
+ 3
you are smart. you are brave. 10 years is most of my life 😂😂 but I see your point about the learning part, but I think telling someone to Google something is an answer, because it'll help them figure out what can be solved by a click and what cannot. For example, this typecasting problem is something that could be easily solved by a Google search, but, say a problem about programming theory or debugging questions can't. What I'm saying is that DavX's point is very valid, but I think that answering questions that cannot be answered in one or two sentences is a better learning experience. My father always says that programming is about deep understanding, and deep understanding is usually not something simple like what built-in functions can do this. Deep understanding is more how and why, how does this work or why does this work. I personally think that is a better way of demonstrating expertise. So in the end, my point has been spoken, this is all I have to say for now :)
12th Sep 2021, 4:08 PM
Chloe
Chloe - avatar
+ 2
11th Sep 2021, 7:20 PM
A͢J
A͢J - avatar
+ 2
Calvin T i never thought of a monarchy as a dictatorship. any form of government can become a dictatorship if enough power is given to one group or individual.
12th Sep 2021, 5:06 AM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 2
➵Chloe[she/her] [DMs Open] [K-POP stan] , this is also for you as well. i worked with an experienced and smart individual that was good at googling everything. i wanted them to continue to expand their learning and told them to use his skills in googling. that was my answer for a number of questions, and i finally got a response from, “this is ridiculous that you are telling us to google this, especially when you know the answer. it would save us hours, days, months if you just spend some time explaing and going over this. no one is learning this, because we are busy doing our work.” this was an eye opener for me. i am forever grateful for this response and have found great fullfillment from spending time with them to share my knowledge, get their feedback, create a connection that not many other people will understand or appreciate, and to watch them learn and grow and even learn from them as well. guess what, know that individal rarely comes to me, they google everything, but we still communicate.
12th Sep 2021, 4:47 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 2
you are smart. you are brave. I'm not entirely sure I've caught your point, doesn't googling it explain it though..?
12th Sep 2021, 4:51 PM
Chloe
Chloe - avatar
+ 2
x=(---) str(x) int(x) float(x) and so on ____
13th Sep 2021, 4:17 PM
Teghveer Singh
Teghveer Singh - avatar
+ 1
DavX As I understood he wants like that casting things.
11th Sep 2021, 7:34 PM
A͢J
A͢J - avatar
+ 1
Calvin T why google when you can access a community full of learners and helpers focused on supporting and promoting a safe environment?
11th Sep 2021, 10:29 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 1
Calvin T as a note, not everyone has the same resources, journey, or support. i think attempting to redirect new learners to a medium that is even more vast than sololearn defeats the purpose of even having solearn. with time and experience, they will eventually learn, but until then why not just provide a helping hand. assuming that they may know what you know, usually only makes everyone in the discussion frustrated.
12th Sep 2021, 4:19 AM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 1
Calvin T i appreciate your attempt to have a discussion and this may not seem like a big issue. but to me, i believe it is an issue that needs to be discussed. by trying to trivialize these micro agressions and justify it is to help others is just a fallacy. im ok if this discussion doesnt “go anywhere”. trying tell someone that you would like to end the discussion at a certain point where you presumably have the last word only shows that that individual thinks they are right and the other person is wrong. when only one perspective in any community overpowers any other perspectives, there is something wrong regardless of how “just”, “pure”, or “fair” they may seem. im not dillusional believing i will change anyones mind in this community, and no one will change my mind. if one perspective can be voiced, other perspectives can also be voiced.
12th Sep 2021, 5:03 AM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 1
Haha, this thread went in a different direction! To add my two cents: I disagree with the dislike of duplicate questions. When a question has been answered, it doesn’t mean it has been perfected (or even correctly answered). With evolving standards and progression, things change. Allowing questions to be reasked helps revaluate these challanges and often highlights newer, better solutions. Secondly for someone who is learning, given the opportunity to answer questions (teach) is a solid way to reinforce what they had learnt. If a question could only be raised once this would dramatically reduce the scope.
12th Sep 2021, 6:54 AM
DavX
DavX - avatar
+ 1
Convert string to: 1) int Num =int(input()) 2) float Num=float(input()) Int to float Num= int(34.0)
12th Sep 2021, 8:03 AM
Rahul Yadav
Rahul Yadav - avatar
+ 1
Woah, this took a turn.. if anyone wants my two cents, I personally think telling someone to Google it is not condescending and/or degrading. If it's something like "isn't it obvious that there's an answer on google", then yeah, freak out in the replies. If it's something in a nice, gentle tone, then I really don't see a problem. Showing them the extensive resources they can find online just by typing into the search bar is a very helpful experience. They may encounter people with their same problem on, say, stackoverflow. They can also find articles and websites that can give them more indepth explanations often times from certified experts. To add on that, google is such an amazing piece of technology, why call someone rude when they're trying to help someone use it in a way that will maximize their chances of getting a quick easy answer? Question answer: There are built in functions in python that can convert one data type to another. You can find more details by searching "python data type convert" :)
12th Sep 2021, 1:42 PM
Chloe
Chloe - avatar
+ 1
Calvin T and ➵Chloe[she/her] [DMs Open] [K-POP stan] it is not that i believe searching in google is bad. i have been searching on google for over 10 years to find solutions. i have also learned that, having the ability to search in google had inadvertantly over inflated my perspective of my own abilities. as an example at one point i thought i was a 10, then eventually realized i was a 5 when i had no access to google. i highly encourage the usage of google. however, when a question is asked, “go do a google search for said question” ,even when put in a polite manner, is not really an answer. it does not create a connection between those that want to learn and those that can teach. and to DavX point, this is an opportunity for me to share what i have learned and reinforce my knowledge. sometimes the best way to learn is to teach someone else. but overall, i have enjoyed this conversation.
12th Sep 2021, 3:56 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 1
Calvin T to me, having your comments marked for deletion is in a way silencing your voice and opinion. i think it is unfortunate that this is done. at the same time i understand there is a want and need to keep this forum somewhat controlled.
12th Sep 2021, 3:59 PM
you are smart. you are brave.
you are smart. you are brave. - avatar