Intro
Welcome to one of our Crash Course series! In this post, we will cover Python.
This course is meant for coders who have already learned a programming language,
but feel free to try if you are up for the challenge.
The idea here is there are some items common to almost every language:
- Variables
- Basic Data Type – string, number, list, dictionary
- Operators
- Control Flow – if/else
- Loops
- Functions
You could also argue classes, but there are many functional-first programming
languages, so we will keep with this list. After this list, you start to get into
more specific language features, like list comprehensions in Python, as well
as more intermediate and advanced features like the standard library and concurrency.
We will briefly cover each of the above topics before using Python. Let’s get started!
Hello World
Let’s start with the simple hello world script. We will introduce the print
function
which is common to many languages.
print("Hello World")
Enter fullscreen mode Exit fullscreen mode
Variables
In python, we can declare variables using the =
assignment operator. You do not
need to specify a type.
name = "Keith"
print(name)
Enter fullscreen mode Exit fullscreen mode
Data Types
Okay, not to get so more details going. Let’s briefly look at each of the basic
data type in python. You should have seen many of these in your previous language.
## Strings test = "Hello, Everoyne!"
print(test)
## Numbers age = 18
print(age)
amount = 12.9
print(amount)
Enter fullscreen mode Exit fullscreen mode
Lists/Arrays
Arrays are called lists in python. You can also have multiple types in a list.
## Creating todo = ["step1", "step2"]
mixedList = [1, 2, "one", "two"]
print(todo)
print(mixedList)
## Acess/reading print(todo[2])
## Updating mixedList[1] = "a new item"
print(mixedList[1])
## Deleting del todo[1]
print(todo)
## Helpful functions print(len(todo))
combined = todo + mixedList
print(combined)
repeated = [4] * 12
print(repeated)
Enter fullscreen mode Exit fullscreen mode
Dictionaries
Maps, Hashmaps, etc are called Dictionaries in Python. They work and much the same way as other languages. You can also
use multiple lines without following normal indention rules.
Notice that you need to use quotes on the key and value for Python Dictionaries.
## Creating profile = {
'name': 'Keith',
'Age': 18,
}
## Accessing print(profile['Name'])
## Updating profile['Age'] = 21
print(profile)
## Deleting del prfile['Name']
Enter fullscreen mode Exit fullscreen mode
Operators
The basic operators are the same in Python as most languages. Let’s take a review of the most common.
first = 10
second = 2
## Math print(first + second)
print(first - second)
print(first * second)
print(first / second)
## Comparisons
# equal to print(first == second)
# no equal print(first != second)
# greater than print(first > second)
# greater than or equal print(first >= second)
# less than print(first < second)
# less than or equal print(first <= second)
Enter fullscreen mode Exit fullscreen mode
Control Flow – if/else
For Python, there are no curly brackets. Python relies on
indentation for blocks and nesting. Let’s see how this works for conditional flows (if, else if, and else).
age = 20
if age > 60:
print("You are to old!")
elif age < 5:
print("You are to young!")
else:
print("Just right")
Enter fullscreen mode Exit fullscreen mode
Notice that we don’t need parenthesis either. We the format is if [condition]:
.
Loops
Python has both a for and while loop. One special keuword in Python is the in
keyword, which let’s you use generators
. This is very helpful when iteratoring of lists, but has many other advantages.
count = 0
while count < 10:
print(count)
count += 1
todoList = ["one", "two", "three"]
for item in todoList:
print(item)
Enter fullscreen mode Exit fullscreen mode
Functions
To create functions in Python, we use the def
keyword followed by the name of the function, parenthesis and the arguments needed.
def getFullName(firstName, lastName):
return firstName + " " + lastName
full = getFullName("Keith", "Holliday")
print(full)
Enter fullscreen mode Exit fullscreen mode
暂无评论内容