Master Python Basics: Data Types, Strings, and Arithmetic Explained
This article provides a concise introduction to Python's core features, covering its interpreted nature, object‑oriented support, dynamic typing, common data types such as strings, numbers, lists, tuples, dictionaries, sets, and booleans, as well as basic string manipulation and arithmetic operations with practical code examples.
1 Python Features
Python is an interpreted, general‑purpose programming language; code is executed line by line.
It supports object‑oriented concepts such as classes and objects.
It is dynamically typed, meaning variable types are determined at runtime.
2 Data Types
String (text) type: "hello world" , 'hello world' , '1' – any characters enclosed in single or double quotes.
Numeric types: integers ( int ) like 1 , 25 , 100000 ; floating‑point ( float ) like 1.0 , 25.1 , 1000.0001 .
Sequence types: list [1,2,3,5,9] , tuple (2,4,6,8) , range range(1,20) (produces numbers 1‑19).
Mapping type: dictionary {'a':1,'b':2} where keys and values are separated by a colon.
Boolean type: True or False .
Set type: {1,2,3,4} – unordered, unique elements, defined with curly braces without colons.
2.1 Viewing Data Types
<code>type('hello world')
str
type(1)
int
type([1,2])
list</code>2.2 Converting Data Types
Convert number to string:
<code>str(20)
'20'</code>Convert numeric string to float:
<code>float('123')
123.0</code>2.3 Displaying Data
Use print() to output values:
<code>print('hello world')
print(1, 'hello', 2)
print((1,3,5))</code>2.4 Variable Assignment
Assign with = :
<code>age = 10
age
10</code>Multiple assignment:
<code>a,b,c = 1, True, [3,4]
a
1
c
[3,4]</code>Chained assignment:
<code>c = d = e = 10
c
10
e
10</code>Note: Variable names cannot contain spaces or start with a digit; use underscores for multi‑word names, e.g., my_name .
3 String Operations
3.1 Creating Strings
<code>name = 'xiaoming'
name1 = "xiaoming"</code>Both single and double quotes are valid; be consistent.
3.2 Indexing
Python uses zero‑based indexing; -1 accesses the last element.
<code>a = 'hello world'
a[0] # 'h'
a[3] # 'l'
a[-1] # 'd'</code>3.3 Length
Use len() to get the number of characters:
<code>len('hello world')
11</code>3.4 Concatenation
Use + to join strings and * to repeat:
<code>'hello world' + '!'
'hello world!'
'hello world' * 3
'hello worldhello worldhello world'</code>4 Numeric Operations
Python supports standard arithmetic operators: + addition, - subtraction, * multiplication, / division, // floor division, ** exponentiation, and % modulus.
<code>4 + 9 # 13
4 - 9 # -5
4 * 9 # 36
4 / 9 # 0.444...
4 // 9 # 0
4 % 9 # 4
4 ** 9 # 262144</code>Beginners can treat Python as a calculator.
5 Summary
This article briefly introduced Python's data types, focusing on strings and numeric operations.
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
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.