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
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
€8.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781789531947
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 99.97
Learn Programming in Python with Cody Jackson
€29.99
Python 3 Object-Oriented Programming
€34.99
Learn Python Programming
€34.99
Total 99.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.