Introduction to Python Variables, Types, and Basic Operations
This article introduces beginners to fundamental computer concepts, explains what a program is, and provides a detailed guide on Python variables, their naming rules, common data types, type‑conversion functions, and basic arithmetic operations with illustrative code examples.
Some Computer Basics
Before learning to program, it is useful to know that a computer consists of five major components: the arithmetic unit, the control unit, memory, input devices, and output devices. The arithmetic and control units together form the Central Processing Unit (CPU), which executes operations and controls instructions. Memory is divided into internal (RAM) and external storage. Input devices (keyboard, mouse, microphone, camera) and output devices (monitor, printer, speaker) are collectively called I/O devices. Modern computers follow the Von Neumann architecture, separating memory from the CPU and representing data in binary.
Binary is a base‑2 counting system where each digit represents a power of two. While programmers do not need to master binary, it is important to understand that all data inside a computer is stored in binary form.
Variables and Types
In programming, a variable is a container that holds data in memory; its value can be read and modified. Python provides several built‑in data types, including integers, floating‑point numbers, strings, and booleans. The most common types are introduced below.
Integer ( int ): Python integers have unlimited precision and can be written in binary ( 0b100 → 4), octal ( 0o100 → 64), decimal ( 100 ), or hexadecimal ( 0x100 → 256).
print(0b100) # 4 print(0o100) # 64 print(100) # 100 print(0x100) # 256Floating‑point ( float ): Represents real numbers and supports scientific notation.
print(123.456) # 123.456 print(1.23456e2) # 123.456String ( str ): Text enclosed in single or double quotes. print('hello') Boolean ( bool ): Two possible values, True and False .
print(True)Variable Naming
Variable names must follow these rules and conventions:
Rule 1: Consist of letters, digits, and underscores; cannot start with a digit. Although Unicode letters are allowed, it is recommended to use only English letters.
Rule 2: Python is case‑sensitive, so A and a are different variables.
Rule 3: Do not use Python keywords or built‑in names (e.g., if, for, print, int).
Convention 1: Use lowercase letters with underscores for multi‑word names (e.g., my_variable).
Convention 2: Prefix a single underscore for protected variables.
Convention 3: Prefix double underscores for private variables.
Using Variables
Example of arithmetic operations with variables:
a = 45 # define variable a b = 12 # define variable b print(a, b) # 45 12 print(a + b) # 57 print(a - b) # 33 print(a * b) # 540 print(a / b) # 3.75Type Inspection and Conversion
The built‑in type() function reveals a variable's type. Conversion functions such as int(), float(), str(), chr(), and ord() change a value from one type to another.
a = 100 b = 123.45 c = 'hello, world' d = True print(type(a)) # <class 'int'> print(type(b)) # <class 'float'> print(type(c)) # <class 'str'> print(type(d)) # <class 'bool'>Examples of type conversion:
print(float(a)) # 100.0 print(int(b)) # 123 print(int('123')) # 123 print(int('123', base=16)) # 291 print(int('100', base=2)) # 4 print(float('123.45')) # 123.45 print(bool('hello')) # True print(int(True)) # 1 print(chr(100)) # 'd' print(ord('d')) # 100Summary
In Python programs, variables store data and can be of different types such as int, float, str, and bool. Built‑in functions allow you to inspect and convert these types, and variables can participate in arithmetic operations, laying the foundation for solving more complex problems in subsequent lessons.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
