Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Python 3 Data Visualization Using ChatGPT / GPT-4

You're reading from   Python 3 Data Visualization Using ChatGPT / GPT-4 Master Python Visualization Techniques with AI Integration

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Mercury_Learning
ISBN-13 9781836649250
Length 314 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Mercury Learning and Information Mercury Learning and Information
Author Profile Icon Mercury Learning and Information
Mercury Learning and Information
Oswald Campesato Oswald Campesato
Author Profile Icon Oswald Campesato
Oswald Campesato
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface
1. Chapter 1: Introduction to Python 2. Chapter 2: Introduction to NumPy FREE CHAPTER 3. Chapter 3: Pandas and Data Visualization 4. Chapter 4: Pandas and SQL 5. Chapter 5: Matplotlib and Visualization 6. Chapter 6: Seaborn for Data Visualization 7. Chapter 7: ChatGPT and GPT-4 8. Chapter 8: ChatGPT and Data Visualization 9. Index

WORKING WITH NUMBERS

Python provides arithmetic operations for manipulating numbers in a straightforward manner that is similar to other programming languages. The following examples involve arithmetic operations on integers:

>>> 2+2
4
>>> 4/3
1
>>> 3*8
24

The following example assigns numbers to two variables and computes their product:

>>> x = 4
>>> y = 7
>>> x * y
28

The following examples demonstrate arithmetic operations involving integers:

>>> 2+2
4
>>> 4/3
1
>>> 3*8
24

Notice that division (“/”) of two integers is actually truncation in which only the integer result is retained. The following example converts a floating point number into exponential form:

>>> fnum = 0.00012345689000007
>>> "%.14e"%fnum
'1.23456890000070e-04'

You can use the int() function and the float() function to convert strings to numbers:

word1 = "123"
word2 = "456.78"
var1 = int(word1)
var2 = float(word2)
print("var1: ",var1," var2: ",var2)

The output from the preceding code block is here:

var1:  123  var2:  456.78

Alternatively, you can use the eval() function:

word1 = "123"
word2 = "456.78"
var1 = eval(word1)
var2 = eval(word2)
print("var1: ",var1," var2: ",var2)

If you attempt to convert a string that is not a valid integer or a floating point number, Python raises an exception, so it is advisable to place your code in a try/except block.

Working with Other Bases

Numbers in Python are in base 10 (the default), but you can easily convert numbers to other bases. For example, the following code block initializes the variable x with the value 1234, and then displays that number in base 2, 8, and 16:

>>> x = 1234
>>> bin(x) '0b10011010010'
>>> oct(x) '0o2322'
>>> hex(x) '0x4d2' >>>

Use the format() function if you want to suppress the 0b, 0o, or 0x prefixes, as shown here:

>>> format(x, 'b') '10011010010'
>>> format(x, 'o') '2322'
>>> format(x, 'x') '4d2'

Negative integers are displayed with a negative sign:

>>> x = -1234
>>> format(x, 'b') '-10011010010'
>>> format(x, 'x') '-4d2'

The chr() Function

The chr() function takes a positive integer as a parameter and converts it to its corresponding alphabetic value (if one exists). The letters A through Z have decimal representation of 65 through 91 (which corresponds to hexadecimal 41 through 5b), and the lowercase letters a through z have decimal representation 97 through 122 (hexadecimal 61 through 7b).

Here is an example of using the chr() function to print uppercase A:

>>> x=chr(65)
>>> x
'A'

The following code block prints the ASCII values for a range of integers:

result = ""
for x in range(65,91):
  print(x, chr(x))
  result = result+chr(x)+' '
print("result: ",result)

NOTE Python 2 uses ASCII strings whereas Python 3 uses Unicode.

You can represent a range of characters with the following line:

for x in range(65,91):

However, the following equivalent code snippet is more intuitive:

for x in range(ord('A'), ord('Z')):

If you want to display the result for lowercase letters, change the preceding range from (65,91) to either of the following statements:

for x in range(65,91):
for x in range(ord('a'), ord('z')):

The round() Function

The round() function enables you to round decimal values to the nearest precision:

>>> round(1.23, 1)
1.2
>>> round(-3.42,1)
-3.4

Formatting Numbers

Python allows you to specify the number of decimal places of precision to use when printing decimal numbers, as shown here:

>>> x = 1.23456
>>> format(x, '0.2f')
'1.23'
>>> format(x, '0.3f')
'1.235'
>>> 'value is {:0.3f}'.format(x) 'value is 1.235'
>>> from decimal import Decimal
>>> a = Decimal('4.2')
>>> b = Decimal('2.1')
>>> a + b
Decimal('6.3')
>>> print(a + b)
6.3
>>> (a + b) == Decimal('6.3')
True
>>> x = 1234.56789
>>> # Two decimal places of accuracy
>>> format(x, '0.2f')
'1234.57'
>>> # Right justified in 10 chars, one-digit accuracy
>>> format(x, '>10.1f')
' 1234.6'
>>> # Left justified
>>> format(x, '<10.1f') '1234.6 '
>>> # Centered
>>> format(x, '^10.1f') ' 1234.6 '
>>> # Inclusion of thousands separator
>>> format(x, ',')
'1,234.56789'
>>> format(x, '0,.1f')
'1,234.6'
lock icon The rest of the chapter is locked
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Python 3 Data Visualization Using ChatGPT / GPT-4
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime
Modal Close icon
Modal Close icon