Data Types and Structures
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things.
Built-in Data Types
Section titled “Built-in Data Types”Python has the following data types built-in by default, in these categories:
- Text Type:
str - Numeric Types:
int,float,complex - Sequence Types:
list,tuple,range - Mapping Type:
dict - Set Types:
set,frozenset - Boolean Type:
bool
Sequences: Lists and Tuples
Section titled “Sequences: Lists and Tuples”Lists are used to store multiple items in a single variable. They are ordered and changeable.
fruits = ["apple", "banana", "cherry"]fruits[1] = "blackcurrant"print(fruits)Tuples
Section titled “Tuples”Tuples are used to store multiple items in a single variable. They are ordered and unchangeable.
coordinates = (10, 20)print(coordinates[0])Mapping: Dictionaries
Section titled “Mapping: Dictionaries”Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered, changeable and does not allow duplicates.
car = { "brand": "Ford", "model": "Mustang", "year": 1964}print(car["brand"])title: Control Flow description: Learn about if-else statements, for loops, and while loops in Python.
Section titled “title: Control Flow description: Learn about if-else statements, for loops, and while loops in Python.”Python supports the usual logical conditions from mathematics.
Conditional Statements
Section titled “Conditional Statements”If-Else
Section titled “If-Else”a = 33b = 200if b > a: print("b is greater than a")elif a == b: print("a and b are equal")else: print("a is greater than b")For Loops
Section titled “For Loops”A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
fruits = ["apple", "banana", "cherry"]for x in fruits: print(x)While Loops
Section titled “While Loops”With the while loop we can execute a set of statements as long as a condition is true.
i = 1while i < 6: print(i) i += 1