Basic Python
Indrasen
Posted on September 15, 2024
Python are used to store and reference various types of data, such as human nouns that refer to people, places, or things. Python has five main data types: numbers, strings, lists, and dictionaries (such as so-called called dicts) and Booleans, these data types are similar in many programming languages. Although they may have different names (for example, lists in Python are called arrays in JavaScript), prominent punctuation and symbols for each data type in Python make it easier to identify. And syntax highlighting in code editors also helps differentiate them.
example
Example 2-1. parts_of_speech.py
# a number is just digits
25
# a string is anything surrounded by matching quotation marks
"Hello World"
# a list is surrounded by square brackets, with commas between items
# note that in Python, the first item in a list is considered to be
# in position `0`, the next in position `1`, and so on
["this","is",1,"list"]
# a dict is a set of key:value pairs, separated by commas and surrounded
# by curly braces
{"title":"Practical Python for Data Wrangling and Data Quality",
"format": "book",
"author": "Susan E. McGregor"
}
# a boolean is a data type that has only two values, true and false.
True
Naming a Python variable
Example 2-2. Naming a Python variable
author = "Susan E. McGregor"
This code tells the computer to set aside a box in memory, label it author, and then
put the string "Susan E. McGregor" into that box. Later on in our program, if we
asked the computer about the author variable, it would tell us that it contains the
string "Susan E. McGregor"
Example 2-3. Printing the contents of a Python variable
# create a variable named author, set its contents to "Susan E. McGregor"
author = "Susan E. McGregor"
# confirm that the computer "remembers" what's in the `author` variable
print(author)
Verbs ≈ Functions
Functions and methods in Python are like verbs in the language. Functions such as print() are implemented and available in the language. While methods are functions associated with a specific data type, for example the split() method works with strings to do things like split text. Built-in functions are simple to use by calling them with the required arguments. In parentheses, however, methods must be attached to specific variables or characters of the relevant data type. Because it's designed to work with that type (e.g. strings have methods like split() but numbers don't).
In the case of the split() method, however, we have to attach the method to a specific
string. That string can either be a literal (that is, a series of characters surrounded
by quotation marks), or it can be a variable whose value is a string. Try the code in
Example 2-5 in a standalone file or notebook, and see what kind of output you get!
refer example in git hub provided link:
example 2-5 & 2-6 in ipynb file
In Python, a user-defined function is a function created by the user to perform specific tasks. Functions allow for code reuse and make programs more modular and organized.
example 2-7 in ipynb file
def greet_me(a_name):
print("Hello "+a_name)
- The def keyword is used to define a function.
- Parentheses after the function name indicate that it is a function and hold any parameters.
- The colon (:) marks the beginning of the indented body of work.
- To get the arguments passed to the function Use the local parameter name in parentheses.
Loops
A loop in Python is a flow control statement that can execute code repeatedly based on a condition or sequence of elements. There are two main types of loops in Python: for loops and while loops.
Types of Loops:
for Loop:
- Used to iterate over a sequence (like a list, string, or range) and execute a block of code for each item in the sequence.
- It automatically moves to the next item in the sequence after executing the code.
while Loop:
- Repeats a block of code as long as a specified condition is True.
- It continues looping until the condition becomes False
Loop Control Statements:
- break: Terminates the loop prematurely.
- continue: Skips the current iteration and moves to the next one.
- pass: Does nothing, used as a placeholder for future code.
example 2-8 to 2-10 in ipynb file
If- else condition
You can use if-else conditions inside loops in Python to perform different actions based on conditions during each iteration of the loop. Both for and while loops can be combined with if-else for control flow.
Posted on September 15, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
August 29, 2023
December 17, 2022