Learn Programming in Python with Cody Jackson
eBook - ePub

Learn Programming in Python with Cody Jackson

Grasp the basics of programming and Python syntax while building real-world applications

Cody Jackson

Condividi libro
  1. 304 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Learn Programming in Python with Cody Jackson

Grasp the basics of programming and Python syntax while building real-world applications

Cody Jackson

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

Kick-start your development journey with this end-to-end guide that covers Python programming fundamentals along with application development

Key Features

  • 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

Book 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.

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

Who this book is 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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Learn Programming in Python with Cody Jackson è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Learn Programming in Python with Cody Jackson di Cody Jackson in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Computer Science e Programming Algorithms. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2018
ISBN
9781789533538
Edizione
1

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 data so they can be used most efficiently. It's easier to write code because the included data structures tend to provide all the features you need, so you spend less time creating your own. If you do need to create your own, you'll probably use the built-in structures as a foundation to start from. This, in turn, means your customized structures will generally perform better than fully customized code, as the built-in data structures have been vetted by multiple developers over a long period of time, so they are fully optimized. Finally, using built-in structures means you always know what is available; proprietary frameworks are an unknown entity, as you can never be sure what is available to you.
In this chapter, we will cover the following topics:
  • Structuring code
  • Common data types
  • Python numbers
  • Strings
  • Lists
  • Dictionaries
  • Tuples
  • Sets
  • Using data type methods
  • Importing modules

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 line 6 else: # if y is False... 7 print("x = true, y = false") # process this line 8 print("x = true, y = unknown") # if x is True, process this line as well 9 else: # if x is False... 10 print("x = false") # process this line 
Each indented line demarcates a new code block. To walk through the preceding code snippet, line 1 is the start of the main code block. Line 2 is a new code section; if x has a value that is true, then indented lines below it will be evaluated. In Python, true can be represented by the word true, any number other than 0, and so on. Hence, lines 3 and 4 are in another code section and will be evaluated only if line 2 is true.
Line 5 is yet another code section and is only evaluated if y is not a false value. Line 6 is part of the same code block as lines 3 and 4; it will also be evaluated in the same block as those lines. Line 9 is in the same section as line 2 and is evaluated regardless of what any other indented lines may do; in this case, this line won't do anything because line 2 is true.
In case that is confusing, the following diagram shows a flowchart of the logic for the previous example. Note that if line 2 is No (the first diamond decision icon), the program logic immediately jumps to line 10 and the program ends. Only if variable X is True will any of the indented lines be evaluated:
Logic flowchart
You'll notice that compound statements, such as the if comparisons, are created by having the header line followed by a colon (:). The rest of the statement is indented below it. The biggest thing to remember is that indentation determines grouping; if your code doesn't work for some reason, double-check which statements are indented. Some development environments allow you to toggle vertical lines that make it easier to check indentation.
The following screenshot is an example of running the program in the preceding example. Notice that, since both x and y are not equal to zero or another false-type value, the if true statements are printed:
White space demonstration

Multiple line spanning

Statements can span more than one line if they are collected within braces (parentheses (), square brackets [], or curly braces {}). Normally parentheses are used. When spanning lines within braces, indentation doesn't matter; the indentation of the initial bracket is used to determine which code section the whole statement belongs to. The following example shows a single variable having to span multiple lines due to the length of the parameters:
tank1 = tank.Tank(
"Tank 1",
level=36.0,
fluid_density=DENSITY,
spec_gravity=SPEC_GRAVITY,
outlet_diam=16,
outlet_slope=0.25
)
tank1 is the variable, and everything to the right of the equal sign is assigned to tank1. While Python allows the developer to write everything within the parentheses on one line, the preceding example has separated each parameter into different lines for clarity.
The Python interpreter recognizes that everything within the parentheses is part of the same object (tank1), so spreading the parameters across multiple lines doesn't cause a problem.
String statements (text) can also be multiline if you use triple quotes. For example, the following screenshot demonstrates a long block of text that is spread over multiple lines:
Triple quote line spanning
When the variable containing the text is directly called (line 10 in the preceding screenshot), Python returns the raw text, including the \n symbol which represents a newline character; it tells the system where a new line starts and the old one ends. However, when the print() function is called in line 11, the text is printed as it was originally entered.

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 also considered built-in types, because they can be passed between scripts, stored in other objects, and otherwise treated like the other fundamental types.
Naturally, you can build your own types if needed, but Python was created so that very rarely will you have to roll your own. The built-in types are powerful enough to cover the vast majority of your code and are easily enhanced.
It was mentioned previously that Python is a dynamic typed language; that is, a variable can be used as an integer, a float, a string, or whatever. Python will determine what is needed as it runs. The following screenshot shows how variables can be assigned arbitrarily. You can also see that it is trivial to change a value, as shown in line 16:
Dynamic typing
Other languages often require the programmer to decide what the variable must be when it is initially created. For example, C would require you to declare x in the preceding program to be of type int and y to be of type string. From then on, that's all those variables can be, even if, later on, you decide that they should be a different type.
That means you would have to decide what each variable will be when you started your program; that is, deciding whether a number variable should be an integer or a floating-point number. Obviously, you could go back and change them at a later time, but it's just one more thing for you to think about and remember. Plus, any time you forgot what type a variable was and you tried to assign the wrong value to it, you would get a compiler error.
Python also has a difference between expressions and statements. In Python, an expression can contain identifiers (names), literals (constant values of built-in types), and operators (primarily arithmetic-looking symbols, but can be other items, such as [], (), or other symbols). Expressions can be reduced to a derived value, much like solving a math equation.
Statements, on the other hand, are everything that can make up a line (or multiple lines) of code; that...

Indice dei contenuti