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

HANDLING USER INPUT

Python enables you to read user input from the command line via the input() function or the raw_input() function. Typically, you assign user input to a variable, which will contain all characters that users enter from the keyboard. User input terminates when users press the <return> key (which is included with the input characters). Listing 1.11 displays the content of user_input1.py that prompts users for their name and then uses interpolation to display a response.

LISTING 1.11: user_input1.py

userInput = input("Enter your name: ")
print ("Hello %s, my name is Python" % userInput)

The output of Listing 1.11 is here (assume that the user entered the word Dave):

Hello Dave, my name is Python

The print() statement in Listing 1.11 uses string interpolation via %s, which substitutes the value of the variable after the % symbol. This functionality is obviously useful when you want to specify something that is determined at run-time.

User input can cause exceptions (depending on the operations that your code performs), so it is important to include exception-handling code.

Listing 1.12 displays the content of user_input2.py that prompts users for a string and attempts to convert the string to a number in a try/except block.

LISTING 1.12: user_input2.py

userInput = input("Enter something: ")

try:
  x = 0 + eval(userInput)
  print('you entered the number:',userInput)
except:
  print(userInput,'is a string')

Listing 1.12 adds the number 0 to the result of converting a user’s input to a number. If the conversion was successful, a message with the user’s input is displayed. If the conversion failed, the except code block consists of a print() statement that displays a message.

NOTE This code sample uses the eval() function, which should be avoided so that your code does not evaluate arbitrary (and possibly destructive) commands.

Listing 1.13 displays the content of user_input3.py that prompts users for two numbers and attempts to compute their sum in a pair of try/except blocks.

LISTING 1.13: user_input3.py

sum = 0

msg = 'Enter a number:'
val1 = input(msg)

try:
  sum = sum + eval(val1)
except:
  print(val1,'is a string')

msg = 'Enter a number:'
val2 = input(msg)

try:
  sum = sum + eval(val2)
except:
  print(val2,'is a string')

print('The sum of',val1,'and',val2,'is',sum)

Listing 1.13 contains two try blocks, each of which is followed by an except statement. The first try block attempts to add the first user-supplied number to the variable sum, and the second try block attempts to add the second user-supplied number to the previously entered number. An error message occurs if either input string is not a valid number; if both are valid numbers, a message is displayed containing the input numbers and their sum. Be sure to read the caveat regarding the eval() function that is mentioned earlier in this chapter.

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 R$50/month. Cancel anytime
Modal Close icon
Modal Close icon