Master Python Basics: Variables, Control Flow, Loops, Lists & Dictionaries
This tutorial introduces Python fundamentals, covering variables, conditional statements, while and for loops, list operations, and dictionary usage, with clear explanations and code examples to help beginners quickly start programming in Python.
21CTO Community Introduction: What is Python? Not a snake, but a powerful, readable programming language created by Guido van Rossum. It emphasizes concise syntax and code readability, allowing developers to express concepts with fewer lines. Python is ideal for data mining, web development, and machine learning; companies like Quora, Google, Pinterest, Spotify, Douban, and Toutiao use it for backend development.
Python Basics
1. Variables
You can think of a variable as a word that stores a value.
Defining a variable in Python is straightforward: one = 1 This assigns the integer 1 to the variable one.
two = 2 some_number = 10000Variables can hold integers, booleans, strings, floats, etc.
true_boolean = True false_boolean = False my_name = "Raymond" book_price = 15.802. Control Flow: Conditional Statements
Use if to evaluate an expression. If it is true, the block runs; otherwise else runs.
if True: print("Hello Python If") if 2 > 1: print("2 if greater than 1")When the condition is false:
if 1 > 2: print("1 is greater than 2") else: print("1 is not greater than 2")You can also chain elif:
if 1 > 2: print("1 is greater than 2") elif 2 > 1: print("1 is not greater than 2") else: print("1 is equal to 2")3. Loops / Iterators
Python supports while and for loops.
While loop example (prints 1‑10):
num = 1 while num <= 10: print(num) num += 1Another while example with a condition variable:
loop_condition = True while loop_condition: print("Loop Condition keeps: %s" % (loop_condition)) loop_condition = FalseFor loop example (prints 1‑10):
for i in range(1, 11): print(i)4. Lists: Collections / Arrays / Data Structures
Lists store ordered collections of items. my_integers = [1, 2, 3, 4, 5] Access elements by index (0‑based):
print(my_integers[0]) # 1 print(my_integers[4]) # 5Lists can hold any type, e.g., strings:
relatives_names = ["Toshiaki", "Juliana", "Yuji", "Bruno", "Kaio"] print(relatives_names[4]) # KaioAdd elements with append:
bookshelf = [] bookshelf.append("The EffectiveEngineer") bookshelf.append("The 4 Hour WorkWeek") print(bookshelf[0]) # The EffectiveEngineer print(bookshelf[1]) # The 4 Hour WorkWeek5. Dictionaries: Key‑Value Data Structure
Dictionaries map keys to values.
dictionary_example = {"key1": "value1", "key2": "value2", "key3": "value3"}Example with personal information:
dictionary_ex = {"name": "Raymond", "nickname": "Luoyi", "nationality": "China"} print("My name is %s" % (dictionary_ex["name"])) # My name is Raymond print("But you can call me %s" % (dictionary_ex["nickname"])) # But you can call me Luoyi print("And by the way I'm %s" % (dictionary_ex["nationality"])) # And by the way I'm ChinaAdd a new key‑value pair:
dictionary_ex["age"] = 38 print(dictionary_ex) # {'name': 'Raymond', 'nickname': 'Luoyi', 'nationality': 'China', 'age': 38}Author: 21CTO Community Note: Original content. Stay tuned for more Python development series.
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
