Python Variables : Python basics for Data Scientist

Python support String and Numbers as primary variable . Here you need not  to declare them before initialization . All you need to initialize when you want to use them .These Python basics for Data Scientist helps to speed up while coding . You may bookmark this article(similar one) to checkout the syntax while coding . Lets understand Python Variables : Python basics for Data Scientist 

Playing with Python Numbers –

There are three kind of Python numbers .

  1. int
  2. float
  3. complexone single image speaks more than 100 words . So lets see –
    Python Variables : Python basics for Data Scientist

     

Playing with Python String –

There is no character variable in Python . In the place of characters , Python has String object . Whether it is a single character or group of character  , It will be a string object .

 

Python Variables : Python basics for Data Scientist (String)

There are so many operations related to python String .For which Python itself provided inbuilt function  For examples –

1. capitalize() and title()

 

When we need to convert our string to capital form . we use capitalize() .

input = “welcome”

x = input.capitalize()

print (x)

 

There can be a situation where we need to capitalize only first letter of each word . In that case we use title().

input = “welcome to data science”

x = input.title()

print (x)

2. casefold() and lower() –

Both works in the same way . They both use to convert string into lower case . Still casefold() is more suitable in large strings .

input = “Introduction To Casefold”

x = input.casefold()

print (x)

Well in the same way you may use lower() .

3.  split() –

In order to split a string into list using some pattern .

txt = “cow#dog#ox”

x = txt.split(“#”)

print(x)

output –

['cow', 'dog', 'ox']

4. encoding and decoding –

In various situation we encode string in different formatting for example –  utf-8 etc . Lets understand how encoding and decoding works in python string . In order to encode you string use the below syntax which is self explanatory .

txt = “hi this is  aåder”

print(txt.encode(encoding=”ascii”,errors=”backslashreplace”)
print(txt.encode(encoding=”ascii”,errors=”ignore”)
print(txt.encode(encoding=”ascii”,errors=”namereplace”)
print(txt.encode(encoding=”ascii”,errors=”replace”)
print(txt.encode(encoding=”ascii”,errors=”xmlcharrefreplace”)
#print(txt.encode(encoding=”ascii”,errors=”strict”)

output –

b'hi this is\\xa0a\\xe5der'
b'hi this isader'
b'hi this is\\N{NO-BREAK SPACE}a\\N{LATIN SMALL LETTER A WITH RING ABOVE}der'
b'hi this is?a?der'
b'hi this is aåder'

I think syntax but must be clear but let me tell you the first parameter is for type of encoding and second is how you would like to deal with error .

 

There is the same way we follow with decoding a string .

 

input = "i love data science"
 
# converting input to base64 encoding
encoded_str = input.encode('base64', 'strict')
# decode the above one –
decoded_str = encoded_str.decode('base64', 'strict')

Conclusion –

There are many more string functions which may make you life easier . In order to know more about them . Please visit the python’s official documentation . Well this is time for tour feed back on the article –  Python Variables : Python basics for Data Scientist . The scope of this article was to introduce you with python variable . As you know python is a dynamically type language which may create run time issue . So it is advisable to use variable in appropriate way .Keep reading articles  with Data Science Learner .
Thanks
Data Science Learner Team 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner