PYTHON DATA TYPES

From this text I’m going to talk about numbers and strings with some of the methods used with these Data Types.

NUMBERS

  • We can simply use numbers in mathematical calculations directly. ‘**‘ is used to get the power of a number.

1

  • Also we can use numbers with floating points as well.

2

  • We can use the str() to convert a number to a string.

3

  • len() is used to find the length of a string.

4

So if we want to get the length of a number, first of all we have to convert it into a string and then find the length of it.

5

6

  • Also there are some built-in mathematical functions as well. We have to import them using the ‘import‘ command before we use them. math class have all the maths methods like pi, sqrt and etc. Random is used to generate a random number.

7.png

8.png

  • Also we can combine methods together.

9

10

STRING

Now let’s look at the string method in python.

  • Just give the value inside ” to make it a string. len() method can be used with string as we talked earlier.

11

  • Also we can use variables to store strings and then using that variable name we can get specific locations value like following.

12.png

13

14

  • Also we can slice strings as well.

15

  • With numbers we used ‘+‘ as a mathematical operator, but with strings we can use it as a string concatenation operator. Also ‘*‘ is used to repeat strings.

16.png

17

  • find() is used to find a specific part of a string. If it found the search value then it will output the indexed value where it found the that searched part.

24

  • replace() can be used to search and replace a part of a string.

26

  • upper() is used to convert a string to uppercase and lower() to convert a string to all lowercase.

29

30

  • Also we can use isalpha() and isdigit() for identify the value type.

31.png

  • split() is used to trim a string from a specific location.

32.png

  • rstrip() is used to remove the extra white spaces from th right hand side of the string.

34

  • Also there are some string formatting techniques as well.

39

37

38

USEFULL

  • There are two special commands used in python where we can get information about methods and available methods for an object type.

40

41

42

43

I tried to give a basic knowledge about some useful methods used with numbers and strings in python. Hope you got an idea about them. See you soon with the rest of the object types used in python. Thank You!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s