Python is a sophisticated general-purpose programming language that is a great place to start learning how to program for beginners. Python features a basic, easy-to-understand syntax that is both concise and readable. Python is a powerful programming language that may be used for a wide range of applications including:
- web development
- software development
- data analysis
- machine learning
- artificial intelligence
Here are the Python basics that will help you in mastering Python the right way!
Python Basics
1. Variables
Defining a variable and assigning a value to it is really simple in Python. Simply define a variable and then add an equal symbol = followed by the value you want to enter. Python variable writing has its own set of rules, which are as follows:
- The first character must be a letter or an underscore.
- The following characters can be letters, underscores, or numbers.
- Variable names have case-sensitive characters. Lowercase and uppercase letters are distinguished in this way. For example, the variables myCourse and MyCourse are different variables. Code snippet for writing variables:
name = "Janet"
print(name)
2. Functions
A function is a code block that only executes when it is invoked. Parameters are data that can be passed into a function. As a result, a function can return data.
Creating a Function
The def keyword is used to declare a function in Python:
def bmi_calculator ():
Calling a Function
Use the function name followed by parenthesis to call it:
def bmi_calculator ():
bmi_calculator()
Arguments
Information can be passed into functions as arguments. Arguments are supplied inside parentheses after the function name. You can use as many arguments as you like; just use a comma to separate them. Here is the code snippet:
def bmi_calculator (name, height, weight):
3. Data Types
Variables can store a variety of data, and different types can perform different tasks. Python comes with the following data types pre-installed in these categories:
Text Type: str
Numeric Types: int
, float
Sequence Types: list
, tuple
, range
Mapping Type: dict
Set Types: set
, frozenset
Boolean Type: true
, false
4. Conditionals
Conditionals are used to anticipate conditions that occur during the course of the program and determine what actions will be taken according to the conditions.
Conditions in Python include if
, else
and elif
The if condition is used to execute code if the condition evaluates to True.
If the condition evaluates to False, the if statement/condition will not be executed.**
Below is an example of using the if condition in Python:
shoe = "Jordans"
if shoe == "Jordans":
print("This is my favorite shoe")
5. Loops
Python has two primitive loop commands:
while loops
for loops
The while Loop
With the while loop we can execute a set of statements as long as a condition is true.
num = 5
while num >= 1:
print("*" * num)
num -= 2
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). With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
for i in range(5):
print(i)
6. Classes and Objects
Creating a class
The class statement is used to create a new class definition. The class name immediately follows the keyword class followed by a colon as follows.
class ClassName: 'Optional class documentation string' class_suite
Creating a class:
class MyClass:
x = 5
Creating an Object
To instantiate a class, you call the class using the class name and pass any arguments that the init method accepts.
The code below shows how that can be achieved:
p1 = MyClass()
print(p1.x)
With the basics, I hope you can create and work with the most essential aspects of Python. Happy coding!
Top comments (0)