**by BehindJava**

# What are representations of numbers in Python with examples.

# Advanced Numbers

In this lecture, we will learn about a few more representations of numbers in Python.
**Hexadecimal**
Using the function hex() you can convert numbers into a hexadecimal format:

```
hex(246)
'0xf6'
hex(512)
'0x200'
```

**Binary**
Using the function bin() you can convert numbers into their binary format.
````
js`
bin(1234)
‘0b10011010010’
bin(128)
‘0b10000000’
bin(512)
‘0b1000000000’
```

```
**Exponentials**
The function pow() takes two arguments, equivalent to x^y. With three arguments it is equivalent to (x^y)%z, but may be more efficient for long integers.
```js
pow(3,4)
81
pow(3,4,5)
1
```

**Absolute Value**
The function abs() returns the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.

```
abs(-3.14)
3.14
abs(3)
3
```

**Round**
The function round() will round a number to a given precision in decimal digits (default 0 digits). It does not convert integers to floats.

```
round(3,2)
3
round(395,-2)
400
round(3.1415926535,2)
3.14
```

Python has a built-in math library that is also useful to play around with in case you are ever in need of some mathematical operations. Explore the documentation here!

# Advanced Strings

String objects have a variety of methods we can use to save time and add functionality. Let’s explore some of them in this lecture:

`s = 'hello world'`

**Changing case**
We can use methods to capitalize the first word of a string, or change the case of the entire string.

```
# Capitalize first word in string
s.capitalize()
'Hello world'
s.upper()
'HELLO WORLD'
s.lower()
'hello world'
```

Remember, strings are immutable. None of the above methods change the string in place, they only return modified copies of the original string.

```
s
'hello world'
```

To change a string requires reassignment:

```
s = s.upper()
s
'HELLO WORLD'
s = s.lower()
s
'hello world'
```

**Location and Counting**

```
s.count('o') # returns the number of occurrences, without overlap
2
s.find('o') # returns the starting index position of the first occurence
4
```

**Formatting**
The center() method allows you to place your string ‘centered’ between a provided string with a certain length. Personally, I’ve never actually used this in code as it seems pretty esoteric…

```
s.center(20,'z')
'zzzzhello worldzzzzz'
```

The expandtabs() method will expand tab notations \t into spaces:

```
'hello\thi'.expandtabs()
'hello hi'
```

**is check methods**
These various methods below check if the string is some case. Let’s explore them:

`s = 'hello'`

isalnum() will return True if all characters in s are alphanumeric

```
s.isalnum()
True
```

isalpha() will return True if all characters in s are alphabetic

```
s.isalpha()
True
```

islower() will return True if all cased characters in s are lowercase and there is at least one cased character in s, False otherwise.

```
s.islower()
True
```

isspace() will return True if all characters in s are whitespace.

```
s.isspace()
False
```

istitle() will return True if s is a title cased string and there is at least one character in s, i.e.

uppercase characters may only follow uncased characters and lowercase characters only cased ones. It returns False otherwise.

```
s.istitle()
False
```

isupper() will return True if all cased characters in s are uppercase and there is at least one cased character in s, False otherwise.

```
s.isupper()
False
```

Another method is endswith() which is essentially the same as a boolean check on s[-1]

```
s.endswith('o')
True
```

**Built-in Reg. Expressions**
Strings have some built-in methods that can resemble regular expression operations. We can use split() to split the string at a certain element and return a list of the results. We can use partition() to return a tuple that includes the first occurrence of the separator sandwiched between the first half and the end half.

```
s.split('e')
['h', 'llo']
s.partition('l')
('he', 'l', 'lo')
```

Great! You should now feel comfortable using the variety of methods that are built-in string objects!