Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Learn Programming in Python with Cody Jackson
Learn Programming in Python with Cody Jackson

Learn Programming in Python with Cody Jackson: Grasp the basics of programming and Python syntax while building real-world applications

eBook
$9.99 $29.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learn Programming in Python with Cody Jackson

Data Types and Modules

Because Python is built upon the C language, many aspects of Python will be familiar to users of C-like languages. However, Python makes life easier because it isn't as low-level as C. The high-level nature of Python means that many data primitives aren't required, as a number of complicated data structures are provided in the language by default.

In addition, Python includes features not often found in low-level languages, such as garbage collection and dynamic memory allocation. On the flip side, Python isn't known for its ability to interact with hardware or perform other low-level work. In other words, Python is great for writing applications but wouldn't be a good choice for writing a graphics card device driver.

Learning how to use built-in data structures helps your programming. Data structures are particular ways of organizing...

Structuring code

Before we get too far into this chapter, we really need to cover the most obvious and special feature of Python: indentation. Python forces the user to program in a structured format. Code blocks are determined by the amount of indentation used; this is frequently referred to as "white space matters". In many other C-based languages, brackets and semicolons are used to show code grouping or end-of-line termination. Python doesn't require those; indentation is used to signify where each code block starts and ends. In this section is an example of how white space works in Python (line numbers are added for clarification). The following code shows how white space is significant:

1  x = 1 
2  if x:  # if x is True... 
3       y = 2  # process this line 
4       if y:  # if y is True... 
5           print("x = true, y = true")  # process this...

Common data types

Like many other programming languages, Python has built-in data types that the programmer uses to create a program. These data types are the building blocks of the program. Depending on the language, different data types are available. Some languages, notably C and C++, have very primitive types; a lot of programming time is spent simply combining these primitive types into useful data structures.

Python does away with a lot of this tedious work. It already implements a wide range of types and structures, leaving the developer more time to actually create the program. Having to constantly recreate the same data structures for every program is not something to look forward to.

Python has the following built-in types:

  • Numbers
  • Strings
  • Lists
  • Dictionaries
  • Tuples
  • Files
  • Sets
  • Databases

In addition, functions, modules, classes, and implementation-related types are...

Python numbers

Python can handle normal long integers (the maximum length is determined based on the operating system, just like C), Python long integers (the maximum length is dependent on available memory), floating-point numbers (just like C doubles), octal and hexadecimal numbers, and complex numbers (numbers with an imaginary component).

Here are some examples of these numbers:

  • Integer: 12345, -32
  • Python integer: 999999999L (in Python 3.x, all integers are Python integers)
  • Float: 1.23, 4e5, 3e-4
  • Octal: 012, 0456
  • Hexadecimal: 0xf34, 0X12FA
  • Complex: 3+4j, 2J, 5.0+2.5j

Historically, integers were 16-bit numbers while longs were 32-bit. This could cause problems when using compiled languages, such as C, because trying to store a number that was too big for its data type could cause errors. The largest 16-bit number available is 65535, so trying to store 999999999 in a regular...

Strings

Strings in programming are simply text; either individual characters, words, phrases, or complete sentences. They are one of the most common elements to use when programming, at least when it comes to interacting with the user. Because they are so common, they are a native data type within Python, meaning they have many powerful capabilities built in. Unlike other languages, you don't have to worry about creating these capabilities yourself.

Strings in Python are different than in most other languages. First off, there are no char types, only single character strings (char types are single characters, separate from actual strings, used for memory conservation). Strings also can't be changed in-place; a new string object is created whenever you want to make changes to it, such as concatenation. This means you have to be aware that you are not manipulating the...

Lists

Lists in Python are one of the most versatile collection object types available. The other workhorses are dictionaries and tuples, but they are really more like variations of lists.

Python lists do the work of most of the data collection structures found in other languages, and since they are built in, you don't have to worry about manually creating them. Lists can be used for any type of object, from numbers and strings to other lists. They are accessed just like strings (since strings are just specialized lists), so they are simple to use. Lists are variable in length; that is, they grow and shrink automatically as they're used, and they can be changed in place; that is, a new list isn't created every time, unlike strings. In reality, Python lists are C arrays inside the Python interpreter and act just like an array of pointers.

The following screenshot...

Dictionaries

Next to lists, dictionaries are one of the most useful data types in Python. Python lists, as previously shown, are ordered collections that use a numerical offset. To select an item in a list, you need to know its position within the list. Python dictionaries are unordered collections of objects, matched to a key name; in other words, you can reference an item simply by knowing its associated key.

Because of their construction, dictionaries can replace many typical search algorithms and data structures found in C and related languages. For those coming from other languages, Python dictionaries are just like a hash table or associative array, where an object is mapped to a key name.

Dictionaries include the following properties:

  • They are accessed by a key, not an offset. Each item in the dictionary has a corresponding key; the key is used to call the item.
  • Stored...

Tuples

The final built-in data type is the tuple. Python tuples work exactly like Python lists except they are immutable; that is, they can't be changed in place. They are normally written inside parentheses to distinguish them from lists (which use square brackets), but as you'll see, parentheses aren't always necessary; however, a comma is always required, as expressions can use parentheses too. Since tuples are immutable, their length is fixed. To grow or shrink a tuple, a new tuple must be created.

Since parentheses can surround expressions, you have to show Python when a single item is actually a tuple by placing a comma after the item. A tuple without parentheses can be used when a tuple is unambiguous. However, it's easier to just use parentheses than to screenshot out when they're optional.

...

Sets

Sets are unordered collections of hashable objects; in other words, each object is unique. Sets are commonly used to see if a collection of objects contains a particular item, remove duplicates from a sequence, and compute a variety of mathematical operations.

Sets look like dictionaries, in that curly braces {} are used to create a set. However, unlike dictionaries, sets only have values; there are no key names within a set.

The following example shows how to create a set:

knights_set = {"Sir Galahad", "Sir Lancelot", "Sir Robin"} 

Sets are also like dictionaries in that the objects they contain are unordered, and it is likely that calling a set will show a different order of objects compared to what was originally set.

There are actually two types of sets: set and frozenset. A regular set is mutable, in that it can be modified in-place. A frozenset...

Using data type methods

Because everything in Python is an object, and the vast majority of objects in Python have methods to provide functionality, this section will discuss some of the more common methods available to Python data types.

Sequence methods

The following methods are common to most sequence types, such as lists, tuples, sets, and strings, except where indicated:

  • x in seq: True if an item within the sequence is equal to x; otherwise, False is returned. This also applies to a subset of a sequence, such as looking for a specific character within a string.
  • x not in seq: True if no item within the sequence is equal to x; otherwise, False is returned.
  • seq1 + seq2: Concatenates two sequences; if immutable sequences...

Importing modules

We briefly touched on importing modules way back in Chapter 1, The Fundamentals of Python. Modules are also called libraries or packages. Modules are modular, often self-contained Python programs that are commonly utilized in other programs, hence the need to import them for access.

Modules are used to separate code to make a program easier to work with, as each module can be designed to do one thing well, rather than having to make a single program that is responsible for all logic.

The Python Package Index (PyPI) website (https://pypi.org) is the official repository of third-party Python libraries. At the time of writing, there are more than 150,000 packages available for download from PyPI. Most of these packages are designed to be imported into a Python project to provide additional, or easier, functionality than can be achieved with the default Python libraries...

Summary

In this chapter, we discussed how to structure Python code, including how to span multiple lines, if necessary. Next, we covered the various data types that are included in Python: numbers, strings, lists, dictionaries, tuples, and sets. We also demonstrated how to use those data types to make simple scripts, and then talked about frequently used methods that provide more functionality to the data types. Finally, we saw how to import modules and how they affect the ability to interact with different parts of Python code.

In the next chapter, we will learn how to control logic flow within a program using if...else statements, looping, and dealing with error exceptions.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Gain a solid understanding of Python programming with coverage of data structures and Object-Oriented Programming (OOP)
  • Design graphical user interfaces for desktops with libraries such as Kivy and Tkinter
  • Write elegant, reusable, and efficient code

Description

Python is a cross-platform language used by organizations such as Google and NASA. It lets you work quickly and efficiently, allowing you to concentrate on your work rather than the language. Based on his personal experiences when learning to program, Learn Programming in Python with Cody Jackson provides a hands-on introduction to computer programming utilizing one of the most readable programming languages–Python. It aims to educate readers regarding software development as well as help experienced developers become familiar with the Python language, utilizing real-world lessons to help readers understand programming concepts quickly and easily. The book starts with the basics of programming, and describes Python syntax while developing the skills to make complete programs. In the first part of the book, readers will be going through all the concepts with short and easy-to-understand code samples that will prepare them for the comprehensive application built in parts 2 and 3. The second part of the book will explore topics such as application requirements, building the application, testing, and documentation. It is here that you will get a solid understanding of building an end-to-end application in Python. The next part will show you how to complete your applications by converting text-based simulation into an interactive, graphical user interface, using a desktop GUI framework. After reading the book, you will be confident in developing a complete application in Python, from program design to documentation to deployment.

Who is this book for?

Learn Programming in Python with Cody Jackson is for beginners or novice programmers who have no programming background and wish to take their first step in software development. This book will also be beneficial for intermediate programmers and will provide deeper insights into effective coding practices in Python.

What you will learn

  • Use the interactive shell for prototyping and code execution, including variable assignment
  • Deal with program errors by learning when to manually throw exceptions
  • Employ exceptions for code management
  • Enhance code by utilizing Python s built-in shortcuts to improve efficiency and make coding easier
  • Interact with files and package Python data for network transfer or storage
  • Understand how tests drive code writing, and vice versa
  • Explore the different frameworks that are available for GUI development

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 29, 2018
Length: 304 pages
Edition : 1st
Language : English
ISBN-13 : 9781789533538
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 29, 2018
Length: 304 pages
Edition : 1st
Language : English
ISBN-13 : 9781789533538
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 130.97
Learn Programming in Python with Cody Jackson
$38.99
Python 3 Object-Oriented Programming
$45.99
Learn Python Programming
$45.99
Total $ 130.97 Stars icon
Banner background image

Table of Contents

13 Chapters
The Fundamentals of Python Chevron down icon Chevron up icon
Data Types and Modules Chevron down icon Chevron up icon
Logic Control Chevron down icon Chevron up icon
Functions and Object Oriented Programming Chevron down icon Chevron up icon
Files and Databases Chevron down icon Chevron up icon
Application Planning Chevron down icon Chevron up icon
Writing the Imported Program Chevron down icon Chevron up icon
Automated Software Testing Chevron down icon Chevron up icon
Writing the Fueling Scenario Chevron down icon Chevron up icon
Software Post-Production Chevron down icon Chevron up icon
Graphical User Interface Planning Chevron down icon Chevron up icon
Creating a Graphical User Interface Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(4 Ratings)
5 star 75%
4 star 0%
3 star 0%
2 star 0%
1 star 25%
Petra Thompson Jan 21, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The uniqueness of Cody Jackson's approach is his ability to demonstrate how programmers tackle real-world programming problems. Readers understand how a programmer thinks and uses the programming language as a problem-solving tool. After doing an exceptional job teaching the syntax and concepts of the Python programming language, he builds upon those principals to culminate in a final project that brings it all together! This is the first basic programming book that I have seen that tackles how to handle SQL queries and database administration. It is my new "go to" book when handling problems related to SQL databases in python! His handling of the power KIVY framework is also impressive. Well done!
Amazon Verified review Amazon
adella klinte Feb 12, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
As a First year student in cyber security, I got this book in hopes of getting ahead in class. This book has surpassed expectations in every way. Programming never came easy to me, but this book gave me every detail needed to learn this language on my own. His unique way of teaching made it not only easy but also enjoyable to learn the language of Python. I hope to use Cody Jacksons books for all other cyber inquiries I may have.
Amazon Verified review Amazon
tadow99 Jan 17, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I came into Python only knowing a little and this book brings you along at a great pace. The hands-on, real-world lessons are amazing and giving you something tangible to grasp. This is just not an a book for academia. The short and easy-to-understand code samples are key to help you grasp what you are writing and how Python works. You are not just copying and pasting syntax; you learn "what" you are doing and "why." I have read through a few "beginner" Python books and this one is the best!
Amazon Verified review Amazon
Alpha Romeo Dec 14, 2020
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Unusable for teaching a teenager learn programming
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.