Python is a high-level, general-purpose, interpreted programming language.
Python is a general-purpose language. It means that you can use Python in various domains including: Web applications, Desktop software, mobile apps, Data science, machine learning, and AI.
Python programs have the extension .py and can be run from the command line by typing python file_name.py.
Install Python on Windows
First, download the latest version of Python from the download page.
Second, double-click the installer file to launch the setup wizard.
In the setup window, you need to check the Add Python 3.8 to PATH and click the Install Now to begin the installation.
Python Hello World
First, create a new folder called helloworld.
Second, launch the VS code and open the helloworld folder.
Third, create a new app.py file and enter the following code and save the file:
print('Hello, World!')
The print() is a built-in function that displays a message on the screen. In this example, it’ll show the message 'Hello, Word!'.
Python Basics
Section 1. Fundamentals
Python Variables
A variable is a label that you can assign a value to it. The value of a variable can change throughout the program.
Use the variable_name = value to create a variable.
The variable names should be as concise and descriptive as possible. Also, they should adhere to Python variable naming rules.
message = 'Hello, World!'
print(message)
In this example, message is a variable. It holds the string 'Hello, World!'. The print() function shows the message Hello, World! to the screen.
Python String
A string is a series of characters. In Python, anything inside quotes is a string. And you can use either single quotes or double-quotes. For example:
message = 'This is a string in Python'
message = "This is also a string"
#multiline string
help_message = '''
Usage: mysql command
-h hostname
-d database name
-u username
-p password
'''
Python Numbers
Python supports common numeric types including integers, floats, and complex numbers.
age = 10 # int
pi = 3.14 # float
total = age + pi # float
print(type(age), type(pi), type(total)) # <class 'int'> <class 'float'> <class 'float'>
Python Boolean
Python boolean data type has two values: True and False.
a = True
b = False
if a is True and b is False:
print("Okay")
Section 2. Operators
Comparison, Logical, and Conditional Operators.
Comparison: ==, !=, <, >, <=, >=
Logical: and, or, not
Conditionals: if, else, elif
Section 3. Control flow
For Loops
Use the for loop statement to run a code block a fixed number of times.
for index in range(5):
print(index)
Section 4. Functions
Python Functions
A Python function is a reusable named block of code that performs a task or returns a value.
Use the def keyword to define a new function. A function consists of function definition and body.
def greet(name):
return f"Hi {name}"
greeting = greet('John')
print(greeting)
#multiple parameters
def sum(a, b):
return a + b
total = sum(10,20)
print(total)
Section 5. Lists
Python Lists
A list is an ordered collection of items.
Python uses the square brackets ([]) to indicate a list. The following shows an empty list:
#list of numbers
numbers = [1, 3, 2, 7, 9, 4]
print(numbers)
#list of strings
colors = ['red', 'green', 'blue']
print(colors)
#Accessing elements in a list
numbers = [1, 3, 2, 7, 9, 4]
print(numbers[1])
#Adding elements in a list
numbers = [1, 3, 2, 7, 9, 4]
numbers.append(100)
print(numbers)
#Inserting elements to a list
numbers = [1, 3, 2, 7, 9, 4]
numbers.insert(2, 100)
print(numbers)
#Removing elements from a list
numbers = [1, 3, 2, 7, 9, 4]
del numbers[0]
print(numbers)
numbers = [1, 3, 2, 7, 9, 4]
last = numbers.pop()
print(last)
print(numbers)
Python Tuples
Tuples are immutable lists.
Use tuples when you want to define a list that cannot change.
A tuple is like a list except that it uses parentheses () instead of square brackets [].
rgb = ('red', 'green', 'blue')
print(rgb)
Section 6. Dictionaries
Python Dictionaries
A Python dictionary is a collection of key-value pairs, where each key has an associated value.
They are surrounded by {} and the value can have any data type.
person = {
'first_name': 'John',
'last_name': 'Doe',
'age': 25,
'favorite_colors': ['blue', 'green'],
'active': True
}
print(person['first_name'])
print(person['last_name'])
Top comments (0)