Python Tutorial: Introduction for beginners (Basic)

CtechF - Python

Python Tutorial: Introduction for beginners (Basic)

1) What is Python?

Python is a scripting language and that is used for creating engineering-analysis tools, web development (server-side), software development, system scripting, and animation software. It designed by Guido van Rossum and first released in 1991. It’s an interpreted, High-level. General-purpose language. Python runs on Windows, Mac OS, and Linux. In this tutorial. You will learn to install and run python on Windows pc.

2) Installation

Step 1: Downloading

Python download link (24.2 MB) – https://www.python.org/

CtechF - Python install
CtechF - Python install

Step 2: Installation

Click on the downloaded Python setup. It will open the Python setup wizard. Click “Install Now” to continue.

CtechF - Python install

After the installation complete. Click close.

Step 3: Get Started

> Run Python in CMD (Command line)

Open CMD

(Press “Windows Key + R” for open run dialog. then type “cmd” and press enter)

CtechF - Python install

Type python –version in the command line and press enter to check Python version.

CtechF - Python install

Type python in the command line and press enter to invoke the python in immediate mode.

CtechF - Python install

Now you can type in the python code and press “enter” to get the output.

Type “exit()” in the command line and press enter to exit.

> Run Python in the IDE (Integrated Development Environment)

When you install Python, an IDE named IDLE is also installed. We can use it to run the python code on the computer. Open Python IDLE.

CtechF - Python install

Now you can type in the python code and press enter to get the output.

3) Python Tutorial

3.1) Comments

One-line comments

# This is a comment

3.2) Docstrings

""" This is a 
docstring """

3.3) Variables

x = 21 #int
y = 5.1 #float
z = 9j #complex
y = "CtechF" #string
  • A variable name can only contain alpha-numeric characters and underscore. (A-z, 0-9, and _)
  • Variable names are case-sensitive.
  • A variable name cannon starts in a number.

Strings

* Get the character at position 6. (First character has the position 0)

a = "Hello World!"
print(a[6])
CtechF - Python Tutorial

* Get the character from position 2 to position 7.

a = "Hello World!"
print(a[2:7])
CtechF - Python Tutorial

* Removes whitespaces from the beginning or the end

a = " My New Website "
print(a.strip())
CtechF - Python Tutorial

* Return the length of a string

a = "ctechf.com"
print(len(a))
CtechF - Python Tutorial

* Return the string in lower case letter.

a = "CtechF.com"
print(a.lower())
CtechF - Python Tutorial

* Return the string in upper case letter.

a = "ctechf.com"
print(a.upper())
CtechF - Python Tutorial

* Replace a string with another string.

a = "ctechf.com"
print(a.replace("c", "O"))
CtechF - Python Tutorial

* Split the string into substrings using a separator.

a = "ctechf.com"
print(a.split("."))
CtechF - Python Tutorial

3.4) Operators

Arithmetic operators

% – Divides a by b and returns remainder (a % b)
// – Divides a by b and returns rounded answer (a // b)
** – Performs power calculation (a ** b -> a to the power b )

CtechF - Python Tutorial

Comparison Operators

CtechF - Python Tutorial

Logical Operators

and – Return true if statements both true,
or – Return true if one or both statements are true,
not – Return true if the result is false,

Identity Operators

3.5) Condition

* if statement

x = 125
y = 225
if y > x:
   print("y is greater than x")
CtechF - Python Tutorial

(if you use if statement without indentation, it will raise an error)

* if-else statement

x = 300
y = 200
if y > x:
  print("y is greater than x")
else:
  print("y is lower than x")
CtechF - Python Tutorial

* if-else-elseif statement (elif)

x = 300
y = 300
if y > x:
  print("y is greater than x")
elif y < x:
  print("y is lower than x")
else:
  print("x and b are equal")
CtechF - Python Tutorial

3.6) Loops

* while loop

i = 1
while i < 10:
  print(i)
  i = i + 1
CtechF - Python Tutorial

* for loop

domain = "ctechf.com"
for x in domain:
  print(x)
CtechF - Python Tutorial
* range()
for x in range(3):
  print(x) # 0, 1, 2
CtechF - Python Tutorial
for x in range(3, 5):
  print(x)
CtechF - Python Tutorial
for x in range(5, 40, 5):
  print(x)
CtechF - Python Tutorial
* For and Else Statement
for x in range(3):
  print(x)
else:
  print("Completed")
CtechF - Python Tutorial
* Nested loop
for x in range(3):
  for y in range(3):
    print(x, y)
CtechF - Python Tutorial

3.7) Python collections

Lists [ ]

Lists are ordered and changeable.

* Create a List
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
print(mobileb)
print(mobileb[2])
CtechF - Python Tutorial
* For Loop Through a List
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
for x in mobileb:
  print(x)
CtechF - Python Tutorial
* Update Item Value
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
print(mobileb)
mobileb[3] =  "Xiaomi"
print(mobileb)
CtechF - Python Tutorial
* Check if Item Exists
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
if "Samsung" in mobileb:
  print("Samsung is in the list")
CtechF - Python Tutorial
* List Length (len())
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
print(len(mobileb))
CtechF - Python Tutorial
* Add Items at the End of the List (append())
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
print(mobileb)
mobileb.append("LG")
print(mobileb)
CtechF - Python Tutorial
* Add Items at the Specified Index (insert())
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
print(mobileb)
mobileb.insert(1, "LG")
print(mobileb)
CtechF - Python Tutorial
* Remove the Specified Item (remove())
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
print(mobileb)
mobileb.remove("Huawei")
print(mobileb)
CtechF - Python Tutorial
* Remove the Specified Index (pop())
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
print(mobileb)
mobileb.pop(1)
print(mobileb)
CtechF - Python Tutorial
* Delete List Item (del)
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
print(mobileb)
del mobileb[3]
print(mobileb)
CtechF - Python Tutorial
* Empties the List (clear())
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
mobileb.clear()
print(mobileb)
CtechF - Python Tutorial
* Delete Complete List (del)
mobileb = ["Samsung", "Apple", "Huawei", "Lenovo"]
del mobileb

Tuple ( )

Tuples are unchangeable and it’s ordered.

* Create a Tuple
os = ("Windows", "Linux", "MacOS")
print(os)
CtechF - Python Tutorial
* For Loop Through a Tuple
os = ("Windows", "Linux", "MacOS")
for x in os:
  print(x)
CtechF - Python Tutorial
* Access Tuple Items
os = ("Windows", "Linux", "MacOS")
print(os[2])
CtechF - Python Tutorial
* Check if Item Exists
os = ("Windows", "Linux", "MacOS")
print(os)
if "Linux" in os:
  print("Linux is in the tuple")
CtechF - Python Tutorial
* Tuple Length
os = ("Windows", "Linux", "MacOS")
print(len(os))
CtechF - Python Tutorial
* Delete Complete Tuple
os = ("Windows", "Linux", "MacOS")
del os

Dictionary { }

Dictionary is changeable, unordered, and indexed

* Create a Dictionary
info = {
  "os":  "Windows 10",
  "ipaddr": "123.211.145.45",
  "status": "online"
}
print(info)
CrKP!-9Wdm4.iU-
* Accessing Dictionary Items (get)
info = {
  "os":  "Windows 10",
  "ipaddr": "123.211.145.45",
  "status": "online"
}
a = info["status"]
print(a)

b = info.get("status")
print(b)
CtechF - Python Tutorial
* Update Dictionary Values
info = {
  "os":  "Windows 10",
  "ipaddr": "123.211.145.45",
  "status": "online"
}
print(info)
info["os"] = "Linux"
print(info)
CtechF - Python Tutorial
* Adding Items
info = {
  "os":  "Windows 10",
  "ipaddr": "123.211.145.45",
  "status": "online"
}
print(info)
info["version"] = "1903"
print(info)
CtechF - Python Tutorial
* Removing items (pop)
info = {
  "os":  "Windows 10",
  "ipaddr": "123.211.145.45",
  "status": "online"
}
print(info)
info.pop("ipaddr")
print(info)
CtechF - Python Tutorial
* Empties the Dictionary
info = {
  "os":  "Windows 10",
  "ipaddr": "123.211.145.45",
  "status": "online"
}
print(info)
info.clear()
print(info)
CtechF - Python Tutorial
* Delete a Dictionary (del)
info = {
  "os":  "Windows 10",
  "ipaddr": "123.211.145.45",
  "status": "online"
}
del info
* Copy a Dictionary (copy())
info = {
  "os":  "Windows 10",
  "ipaddr": "123.211.145.45",
  "status": "online"
}
print(info)
newdict = info.copy()
print(newdict)
CtechF - Python Tutorial

this copy method also works in list and tuple

* Copy a Dictionary (dict)
info = {
  "os":  "Windows 10",
  "ipaddr": "123.211.145.45",
  "status": "online"
}
print(info)
newdict = dict(info)
print(newdict)
CtechF - Python Tutorial

Set { }

sets are unordered and unchangeable.

* Create a Set
country = {"The United Kingdom", "United State", "Canada", "China"}
print(country)
CtechF - Python Tutorial
* Accessing Items
country = {"The United Kingdom", "United State", "Canada", "China"}
for x in country:
   print(x)
CtechF - Python Tutorial
* Check if key Exists
country = {"The United Kingdom", "United State", "Canada", "China"}
print("China" in country)
CtechF - Python Tutorial
* Add Items (add())
country = {"The United Kingdom", "United State", "Canada", "China"}
print(country)
country.add("Japan")
print(country)
CtechF - Python Tutorial
* Add Multiple items (update())
country = {"The United Kingdom", "United State", "Canada", "China"}
print(country)
country.update(["Sri Lanka",  "Indonesia", "Vietnams" ])
print(country)
CtechF - Python Tutorial
* Copy a Set
country = {"The United Kingdom", "United State", "Canada", "China"}
print(country)
newset = country.copy()
print(newset)
CtechF - Python Tutorial
* Set Length
country = {"The United Kingdom", "United State", "Canada", "China"}
print(country)
print(len(country))
CtechF - Python Tutorial
* Empties the set
country = {"The United Kingdom", "United State", "Canada", "China"}
country.clear()
print(country)
CtechF - Python Tutorial
* Removing items
country = {"United Kingdom", "United State", "Canada", "China"}
print(country)
country.remove("United Kingdom")
print(country)
CtechF - Python Tutorial
* Delete the Set
country = {"The United Kingdom", "United State", "Canada", "China"}
del country

3.8) Create a First Python Program

Open Python IDLE and Navigate File -> New File (CTRL + N)

Type following code.

#Input
print("Enter your website domain: ")
A = input()
print("Website is, ", A)
CtechF - Python Tutorial

Save the file as input.py and run it through the cmd or press F5

CtechF - Python Tutorial
CtechF - Python Tutorial
CtechF - Python Tutorial

Sources: https://pixabay.com (Images)

Hi Friends,

Hope this post (Python Tutorial) will help you to learn something, If you enjoy my work then please share my posts with your buddies and anyone who might be interested in programming and don’t forget to subscribe.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page : https://www.facebook.com/ctechf

Also Read :

Leave a Reply

95 Shares
Share
Tweet
Share
Pin
Share