What is python?
Python is an interpreted, general-language, high-level programming language invented by Guido van Rossum and was conceived in the late 1980s and released in 1991.
Python is easy to read and write
This is because since its syntax emphasizes readability thus easy and simple to read and write.
Lets see what makes it simple.
Sample helloworld.
In PHP
<?php
echo "Hello World!";
?>
Above is a three line code in Php.
Lets see same code in Python.
print("Hello World!")
3 lines of code in PHP to one line of Code in python.
Lets check how to install python in different environments
Python installation steps
- 1. Visit https://www.python.org/downloads/
- 2. Select your Operating system(Windows,Linux/UNIX,MacOS,Other
- 3. Select the release or version, click on it to download.
- Double click on the file to execute and install. -- For window mark “add to system paths” An alternative way to install python on Linux is to run sudo apt install python3 or sudo apt install python on the terminal. For more check on the python Documentationfor more documentations.
Lets now test our installation
- Open Terminal on Linux or cmd on windows
- Run the python --version command to check if you installed the correct version. If python is installed you will se something similar to below
C:\Users\Florida>python --version
Python 3.10.2
Lets now start learning python.
On your terminal type Python hit enter and then type print("Hello Your_name!")
python
print("hello your_name!")
See below expected output. Remember to use your name after hello
We have different Platforms where you can start writing your codes on. Including Visual Studio code, Pycharm, Sublime-text and much more. Select a platform where you are best suited of and lets continue learning python.
Lets see how Variables are declared also need to check on best practises guidelines on PEP 8 -- Style Guide for Python Code
Variables-are containers for storing data values.
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it
Here i will be using pycharm to write my sample code.
I have created a file example.py
Have declared 2 variables as below.
a = "I"
b = 10
print(a)
print(b)
See below expected output
Also you don't need to declare type.
a ="I" is of type str
a =10 is of type int
If you need to know the type you just do as below
print(type(a))
Output
<class 'int'>
Casting
This is done if you want to specify the data type of your variable.
a = int(10)
Case sensitive
Note Variable names are case sensitive
a = 10
A = "John"
a cannot overwrite A since python is case sensitive
Let now check the type of variables:
- Local variables
- Global Variables
Local Variables:
This are variables declared inside a function and without global keyword.
def myfunc():
a="Francis"
This variables are accessed inside but not outside the function.
Global Variables:
This are variables created outside of a function and can be used by everyone, both inside of functions and outside.
a = "Kenya"
def myfunc():
print("I live in " + a)
myfunc()
Output
I live in Kenya
You can also create a global variable using global keyword
def myfunc():
global a
a = "kenya"
myfunc()
print(a)
Hope this guide gives you an Introduction to Python 101 .
Let me know your thoughts in the comment section 👇
Like, share and follow me 🚀 for more content.
Top comments (0)