Python is one of few coding languages, which is easy to deploy and use for day to day tasks, whether in DevOps for configuration management or Cloud Infra for health checks and instance life cycle management. What I like most about python is its easy for anyone to switch over and get their hands dirty in learning it with lesser effort compared to other like Javascript or C/C++.
I actually started learning python because, why not? Its everywhere on whichever VM or Kubernetes cluster I install, so not much effort to install and create lab, plus you can easily check its implementation realtime whether with ansible playbooks or openstack cloud infrastructure. So lets rock and roll into basics, shall we.
1. Print
As name implies, print function is used to print any sort of output. While printing, we deal with three basic types of data,
- Integer – 10, 12, 3555 etc
- Floating Point – 3.5, 4.1, 15.444 etc
- String – Basically any word or letter, Hello World etc
Code/Output
>>> print("Technokopi is cool")
Technokopi is cool
>>> print(10)
10
>>> print(10.6)
10.6
Code/Output
>>> print (f"Black Coffee is {2*3+10} times cooler than Latte,\nCrazy Right?")
Black Coffee is 16 times cooler than Latte,
Crazy Right?
In above example, \n is used for start of new line while f , function used to print string and integer together.
2. Variables
Just like in Ansible playbooks, defining variables in start of your code can save time in writing repetitive stings or other data types, if they are repeated over course of script. Variables used in python don’t need any syntax to let program understand it is variable function, instead we can work just like simple key value pairs.
Code/Output
>>> good_coffee = "black"
>>> bad_coffee = "latte"
>>> how_many_times = 2*3+10
>>> print(f"I always find {good_coffee} {how_many_times} times cooler than {bad_coffee},\nCrazy Right?")
I always find black 16 times cooler than latte,
Crazy Right?
3. Define Functions
Functions are group of codes written together under common umbrella and later called upon whenever required. Functions start with def (define) syntax. (Note: From now onwards I will be writing code in seperate .py file using vi editor so syntax may look different)
Code
good_coffee="black"
bad_coffee="latte"
how_many_times=2*3+10
def what_you_like():
print(f"I feel {good_coffee} is {how_many_times} times better than {bad_coffee},\nCrazy Right?")
what_you_like()
Output
I feel black is 16 times better than latte,
Crazy Right?
() brackets indicates to python that we are defining a function. Additionally these brackets can be used to define “Parameters” which help us in providing input variable values to be used by function whenever its called later.
Code
good_coffee="black"
bad_coffee="latte"
how_many_times=2*3+10
def what_you_like(more_times):
print(f"I feel {good_coffee} is {how_many_times} times better than {bad_coffee} or may be {more_times} times,\nCrazy Right?")
what_you_like(100)
Output
I feel black is 16 times better than latte or may be 100 times,
Crazy Right?
We can repeat call for same function with different values in different lines to give multiple outputs.
Code
what_you_like(100)
what_you_like(200)
******************OUTPUT*****************
I feel black is 16 times better than latte or may be 100 times,
Crazy Right?
I feel black is 16 times better than latte or may be 200 times,
Crazy Right?
Output
what_you_like(100)
what_you_like(200)
******************OUTPUT*****************
I feel black is 16 times better than latte or may be 100 times,
Crazy Right?
I feel black is 16 times better than latte or may be 200 times,
Crazy Right?
Checkout our next blog in this python3 learning series to know more about using python language.