• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 4 fuera de 304 páginas
Presentación

Think Python, 3rd Edition

Document preview thumbnail
Vista previa 4 fuera de 304 páginas

"Python is an excellent way to get started in programming, and this clear, concise guide walks you through Python a step at a time—beginning with basic programming concepts before moving on to functions, data structures, and object-oriented design. This revised third edition reflects the growing role of large language models (LLMs) in programming and includes exercises on effective LLM prompts, testing code, and debugging skills. With this popular hands-on guide at your side, you'll get: A grounding in the syntax and semantics of the Python language A clear definition of each programming concept, with emphasis on clear vocabulary How to work with variables, statements, functions, and data structures in a logical progression Techniques for reading and writing files and databases A solid understanding of objects, methods, and object-oriented programming Debugging strategies for syntax, runtime, and semantic errors An introduction to recursion, interface design, data structures, and basic algorithms How to use LLMs—including effective prompts, testing code, and debugging And more"

Vista previa del contenido

,Chapter 1. Programming as a Way of
Thinking
The first goal of this book is to teach you how to program in Python. But learning to program
means learning a new way to think, so the second goal of this book is to help you think like a
computer scientist. This way of thinking combines some of the best features of mathematics,
engineering, and natural science. Like mathematicians, computer scientists use formal languages
to denote ideas—specifically computations. Like engineers, they design things, assembling
components into systems and evaluating trade-offs among alternatives. Like scientists, they
observe the behavior of complex systems, form hypotheses, and test predictions.

We will start with the most basic elements of programming and work our way up. In this chapter,
we’ll see how Python represents numbers, letters, and words. And you’ll learn to perform
arithmetic operations.

You will also start to learn the vocabulary of programming, including terms like operator,
expression, value, and type. This vocabulary is important—you will need it to understand the rest
of the book, to communicate with other programmers, and to use and understand virtual assistants.


Arithmetic Operators
An arithmetic operator is a symbol that represents an arithmetic computation. For example, the
plus sign, +, performs addition:


30 + 12


42


The minus sign, –, is the operator that performs subtraction:


43 - 1


42


The asterisk, *, performs multiplication:


6 * 7

,42


And the forward slash, /, performs division:





42.0


Notice that the result of the division is 42.0 rather than 42. That’s because there are two types of
numbers in Python:

 integers, which represent whole numbers, and
 floating-point numbers, which represent numbers with a decimal point.

If you add, subtract, or multiply two integers, the result is an integer. But if you divide two integers,
the result is a floating-point number. Python provides another operator, //, that performs integer
division. The result of integer division is always an integer:


84 // 2


42


Integer division is also called “floor division” because it always rounds down (toward the “floor”):


85 // 2


42


Finally, the operator ** performs exponentiation; that is, it raises a number to a power:


7 ** 2


49


In some other languages, the caret, ^, is used for exponentiation, but in Python it is a bitwise
operator called XOR. If you are not familiar with bitwise operators, the result might be unexpected:

, 7 ^ 2


5


I won’t cover bitwise operators in this book, but you can read about them
at http://wiki.python.org/moin/BitwiseOperators.


Expressions
A collection of operators and numbers is called an expression. An expression can contain any
number of operators and numbers. For example, here’s an expression that contains two operators:


6 + 6 ** 2


42


Notice that exponentiation happens before addition. Python follows the order of operations you
might have learned in a math class: exponentiation happens before multiplication and division,
which happen before addition and subtraction.

In the following example, multiplication happens before addition:


12 + 5 * 6


42


If you want the addition to happen first, you can use parentheses:


(12 + 5) * 6


102


Every expression has a value. For example, the expression 6 * 7 has the value 42.


Arithmetic Functions

Información del documento

Subido en
9 de agosto de 2024
Número de páginas
304
Escrito en
2017/2018
Tipo
Presentación
Personaje
Desconocido
$5.49

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
0
Seguidores
0
Artículos
225
Última venta
-



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes