Curso Python 3 desde cero #3 | Variables en Python
Introduction to Variables in Python
Understanding Variables
- A variable in programming is a memory space for storing and retrieving data used by a program.
- Each variable must have a unique name that does not conflict with Python's reserved commands, and it cannot contain spaces.
- The two most common types of variables are those that store numbers (integers
intand floating-point numbersfloat) and those that store text, known as strings.
String Representation
- Strings must be enclosed in quotes; for example, "Hello World" is recognized as text by Python.
- A variable acts as a container where specific data can be stored, such as integers, floats, or strings.
Declaring Variables
Variable Declaration Process
- To declare a variable in Python, you first assign it a name followed by an equal sign to indicate the value to be stored.
- The assignment operator (
=) tells the program to prepare memory space for the specified value.
- For instance, assigning
number_one = 2stores the integer 2 in the memory space labelednumber_one.
Data Type Identification
- Python automatically identifies the type of data being stored without explicit declaration from the programmer.
- If storing an integer or float, Python allocates appropriate characteristics for that memory space based on the assigned value.
Case Sensitivity in Variable Names
Differentiating Variable Names
- Python distinguishes between uppercase and lowercase letters; thus
Number_Oneandnumber_oneare treated as different variables.
- This feature allows programmers to use similar names while maintaining distinct identities for each variable.
Executing Programs with Variables
Program Execution Overview
- When executing a program, instructions like
print()direct output to display messages on screen.
Understanding Basic Python Operations
Introduction to Program Execution
- The program begins by printing "esto es una suma" on the screen, a concept previously covered in an earlier video about compiling and executing programs.
- After completing the print instruction, the program moves to create a variable named
numero_1, which will store the value 2.
Variable Creation and Memory Allocation
- The program allocates memory for
numero_1and assigns it the value of 2 using the assignment operator.
- Next, it creates another variable called
numero_2, which is designated to hold the value 4.
Performing Calculations
- Following this, a new variable named
resultadois declared to prepare for storing the sum ofnumero_1andnumero_2.
- The execution process involves calculating the sum of values stored in both variables (
numero_1 + numero_2) and saving this result inresultado.
Outputting Results
- Once calculated, the program returns to execute a final instruction that prints out the content of
resultado, which holds the number 6.
- The output occurs after displaying "esto es una suma," confirming that Python successfully executed all instructions.
Practical Application: Coding Example
- To better understand these concepts, practical coding steps are demonstrated using an editor where users can write their code.
- The first line of code uses
print()with double quotes around "esto es una suma" to display text on screen. Single quotes could also be used interchangeably.
Variable Naming Conventions
- When declaring variables like
numero_1, it's emphasized that spaces are not allowed; underscores or camel case should be used instead.
- A second variable,
numero_2, is created next to store the value 4.
Finalizing Code Instructions
How to Create and Run a Simple Python Program
Writing the Program
- The speaker introduces the process of printing the result of a sum in Python, emphasizing the use of
print()without quotes since an integer value is being displayed.
- The variable containing the sum result is referenced directly within the parentheses of the
print()function, demonstrating how to structure a simple program that performs addition.
Saving the Program
- Instructions are provided on saving the program by clicking on "File" and then "Save," which opens a file explorer for selecting a save location (e.g., Desktop).
- The importance of naming conventions is highlighted; for example, naming the file "suma.py" with a
.pyextension, which is essential for Python files.
Running the Program
- After saving, users are guided to execute their program by selecting "Run" or pressing F5. This action sends commands to the Python interpreter.
- Upon execution, it displays both an introductory message ("this is a sum") and shows the calculated result from earlier operations.
Opening Existing Programs
- The speaker explains how to open previously saved Python programs by navigating through "File" and selecting "Open," leading back to where files are stored.
- Once opened, users can view their previously written code. This reinforces that using variables in Python is straightforward and will become more complex as they progress in learning.
Conclusion and Resources