This content originally appeared on DEV Community and was authored by Gitau20
Introduction To Python
Python is an interpreted high-level language created by Guido Van Rossum and released in 1991. Python is the best solution in many domains from web application, data analysis, data science , machine learning, Automation, data wrangling and AI.
Common Features Provided By Python
Simplicity: Think less of the syntax of the language and more of the code.
Open Source: A powerful language and it is free for everyone to use and alter as needed.
Portability: Python code can be shared and it would work the same way it was intended to, seamless and hassle-free.
Being Embeddable & Extensible: Python can have snippets of other languages inside in to perform certain functions.
Being Interpreted: The worries of large memory tasks and other heavy CPU tasks are taken care of by Python itself leaving you to worry only about coding.
Huge Amounts of Libraries: Python has you covered in Data Science & Wed Development.
Object Orientation: Objects help breaking down complex real-life problems such that they can be coded and solved to obtain solutions.
Python Basics
Section 1: Program Flow
A program could be long and complicated but it is built of simple parts.
Variable: Helps keep track of the information we need to successfully execute a program.
my_name = 'Liz'
my_age = 25
my_favorite_number = 3
has_pet= False
print('My name is', my_name)
print('My age is', my_age)
print('My favorite number is', my_favorite_number)
print('I own a pet:', has_pet)
String: Is a sequence of characters. Integer refers to whole numbers. A floating number refers to decimal number. Bool or boolean refers to either True or False.
Functions: Allow us to define a task we would like the computer to carry out based on input. We define functions using def. For example, write a function which takes in a say-hello variable and print out "Hello World".
Write a function, say_hello which takes in a name variable and print out "Hello name". say_hello("World") should print "Hello World".
def say_hello(name):
print("Hello {}".format(name)
say_hello("World"))
Syntax: This refers to the rules of organizing code to determine which part is the function and which part is the argument.
def codey():
print('codey is running')
print('exiting without returning a value')
codey()
Conditionals and Logic: We'll want the computer to take action only if under certain conditions. Logic is determined to return either true or false.
x = 6
y = 2
print(x > 4 and y > 1)
print(x > 7 and y > 1)
print(x > 7 or y > 1)
Iteration: This refers to repetitive loops to execute the same code as many times as possible. Most basic is while loop which keeps on executing as long as the condition after while is true.
x = 0
while x < 10:
print(x)
x = x + 1
Section 2: Data Structures (Lists, Tuples, Dictionary & Sets)
List: Ordered collection of items using square bracket. Let's try creating a list of to-do.
todo= ['exercise', 'cleaning', 'assignments']
todo.append('eat')
print(todo)
Tuples: It is similar to a list with major difference that it is immutable. We create using parentheses ()
todoo= ('cook','clean','code','sleep')
print(todoo)
print(todoo[0:2])
Dictionary: Involves a key associated with a value in a key-value pair. The key-value pair are in {} syntax to create a dict. Each key-value pair is separated by a comma, and within a pair the key and value are separated by a colon :
user = {
"name" : 'Liz',
"age" : 25,
"height" : "173"
}
print(user)
print(user['age'])
Set: Similar to a list except that it is unordered. It can store heterogenous data and it is mutable. Creating a set enclosed in a curly bracket {}.
set1 = {'Liz', 25, 173, True}
print(set1)
print(set1.pop())
print(set1)
The print out is ordered differently from the input. We can add or remove items in the set.
set1.add('True')
print(set1)
set1.update([59.2, 'black'])
print(set1)
With a kick start to basic coding, have fun at it!
This content originally appeared on DEV Community and was authored by Gitau20
Gitau20 | Sciencx (2022-02-14T02:07:28+00:00) INTRODUCTION TO MODERN PYTHON. Retrieved from https://www.scien.cx/2022/02/14/introduction-to-modern-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.