Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 24, 2022 08:21 am GMT

Python 101: The Ultimate Python Tutorial for Beginners

Python is a high-level language that was designed by Guido Van Rossum and released in 1991.

Over the years, Python has grown to be popular and tops many charts as it gains more users. It is termed as a Beginner-friendly language because of its simple syntax and how easy it could be to master it.

Python has become one of the most reliable programming languages in different fields of specialization. From Data Science, Machine Learning, Artificial Intelligence to software engineering; Python is one of the best languages for a beginner who aims to learn programming or add something cool to their portfolio.

What are some of the features of Python?

  • Object-Oriented Language: Your python program consists of set of objects that you used to solve a problem.
  • Easy-to-learn language: Python is a language that is easy to learn and doesn't put pressure on a beginner desiring to learn programming.
  • Interpreted language: Python is an interpreted language. It won't require you to manually compile and execute your program; just write code and run.
  • Free and Open Source language: Python is an open-source language that is free for everyone to learn and use.
  • Huge Standard Library: Python has you covered with a cool standard library that supports various specialization fields like web dev and data science.

This are just some of the features of Python programming language.

How to install and set-up Python:

You can download the latest version of Python for your PC or laptop from here. Versions for Windows, Linux and MacOS are available.

You can go through a guide on installation of Python on various OS platforms from here and also know how to configure Python for VSCode from here.

Note: There are various websites you can use to learn Python. For starters, I would recommend Programiz and GeeksforGeeks for learning the basics of Python and some advanced topics.

Commonly used IDEs for Python

There are two commonly-used IDEs that are beginner-friendly. They are:
*VSCode
*PyCharm Community Edition

Writing your first Python program: Hello World!

This first program is very easy and shows a portion of what will be dealt with. Check out the code:

print("Hello World!")

The code consists of print() which is a built-in function used to print expressions(strings, numbers, etc.) written within parentheses and "Hello World!" string.
The code output after running the program is:

Hello World!

Comments

Comments are used as to describe what happens at some point in the code of a program or to leave a message for another developer modifying the code. It's very meaningful in a code-space. Python comments have their own syntax:

#This is a single-line comment"""This is amulti-linecomment, usually referred to as Docstring"""

Variables

Declaring variables in Python isn't as complicated or very precise like in C++ where you specify the data-type before declaring your variable.

name = "John Doe"age = 21

As displayed in the code block above, you declare a variable by simply declaring a variable name and add a value to it and you're set to proceed.
The output:

John Doe21

When writing strings, you could use single quotation marks(' ') or double quotation marks(" "). Both work perfectly.

Taking input from user

Take for example, you want to create a program that takes a user's name and age and returns a statement welcoming them. You will need to take input from the user interacting with the program. You can do that with ease as shown below:

name = input("Enter your name: ")age = int(input("Enter your age: "))print("Welcome,", name, ". You are", age, "years old!" )

We declare the variable 'name' and request the user to enter their name. The same is done for 'age' but the data-type is specified because the input() function takes strings by default.
After the user inputs their name and age as 'James Doe', 30, the output becomes:

Welcome, James Doe . You are 30 years old!
Concatenation of strings with variables

Concatenating of strings with variables can be done in various ways. The variables used in Taking input from user shall be used to explain concatenation

  1. Use of the addition (+) operator:
"""While using addition operator to concatenate, convert all int variables to string with str() function"""print("Welcome, " + name + ". You are " + str(age) + " years old!")

Output:

Welcome, James Doe. You are 30 years old!
  1. Use of comma(,)
print("Welcome,", name, ". You are", age, "years old!")

Output:

Welcome, James Doe . You are 30 years old!

Python Operators

Python operators serve different purposes as listed below:
Arithmetic: These operators are used to perform arithmetic operations in Python. They include: +, -, //, %, **, *, /

print(5 + 2) #additionprint(5*7) #multiplicationprint(2**5) #exponentialprint(18/2) #divisionprint(3%4) #modulusprint(25//5) #floor divisionprint(5 - 4) #subtraction

Output:

735329.0351

Comparison: These operators are used to compare two things and return a boolean result. They include: >, <, <=, >=, ==, !=

x = 20y = 10print(x < y) #less thanprint(x > y) #greater thanprint(x <= y) #less than or equals toprint(x >= y) #greater than or equals toprint(x != y) #not equal toprint(x == y) #equals

Output:

FalseTrueFalseTrueTrueFalse

Logical: These operators are used to combine conditional statements. They include: and, or, not

x = 20y = 10print(x < 30 and y > 9)print(x > 10 or y < 5)print(not(x < 5 and y < 10))

Output:

TrueTrueTrue

Identity and membership: Identity operators return true if a both values are the same and false if otherwise. They include: is, is not. Membership operators return true is an object is present in another and false if otherwise. They include: in, not in.

x = 20y = 10#identityprint(x is y)print(x is not y)#membershipprint("ment" in "mentality")print("mint" not in "mentality")

Output:

FalseTrueTrueTrue

Python statements and loops

If...else and if...elif statements
You want to create a Python program that does one thing if input is true and does another if input is false; that is where you implement the if...else and if...elif statements.

  • If...else statement:
age = int(input("How old are you: "))if age < 13:    print("You are not allowed to play this game!")else:    print("You can proceed to the arena.")

Output:
Output depends with the input given by the user. If age
is greater than 13, you shall proceed but if otherwise, you will not be allowed to play the game.

  • If...elif statement:
marks = int(input("Enter your score: "))if marks >= 70:    print("Pass")elif marks >= 50:    print("Average")else:    print("Good try! You can do better next time!")

Output:
If marks are 70 and above, output will be "Pass. If they are from 50 to 69, output will be "Average while if 49 and lower, output becomes "Good try!..."

Python Loops

  1. For Loop

For loop in Python is used for iterating over a sequence.

students = {"Zelda", "John", "Jane", "Simon"}for student in students:    print(student)

Output:

SimonJohnZeldaJane
  1. While Loop

While loop executes a set of statements as long as the condition is true or satisfied. While loops can execute to infinity so it is important to use increments or break statement.

n = 10count = 0while count < n:    count += 1 #increment added so loop can break after reaching n    print(count)

Output:

12345678910

Python Data structures

  • Lists

Lists, commonly known as arrays, are data structures with changeable sequences of elements. Elements inside a list are called items. They are defined by square brackets - [ ].

market = ["books", "groceries", "bread", "electronics"]print(market)

Output:

['books', 'groceries', 'bread', 'electronics']
  • TuplesTuple is a data structure that stores an ordered sequence of elements. They cannot be changed once or modified like lists.They are described by parentheses - ( ).
market = ("books", "groceries", "bread", "electronics")print(market)

Output:

("books", "groceries", "bread", "electronics")
  • SetsSet is a data structure used to store multiple elements in a single variable. They are unindexed, unordered and cannot be changed. They are described by curly brackets - {}.
students = {"Zelda", "John", "Jane", "Simon"}print(students)

Output:

{"Zelda", "John", "Jane", "Simon"}#note: order of elements might change as sets are unordered.
  • DictionariesDictionary is a data structure that stores elements in a key:value pairs. They are also known as associative arrays and described by curly brackets - {}.
students = {1: "Zelda", 2: "John", 3: "Jane", 4: "Simon"}print(students)

Output:

{1: "Zelda", 2: "John", 3: "Jane", 4: "Simon"}

Python user-defined functions

A python user can declare functions using the def keyword and thereafter, give their function a name, include arguments if any present inside parentheses and define what the function would do.
Even without arguments, always include parentheses for environment to identify that a function is being declared.

#declaring the functiondef powers(y, x):    n = y**x    return n#call the function and give arguments to get output powers(2, 2)

Output:

4

Python Classes

Python is an object-oriented programming language and thus, the introduction of classes and objects.

  • self parameter is used to access variables used in a class
  • init() function is used to assign values to object properties.
class person:         #declaring a class that takes user name and age    def __init__(self, name, age): #initializing properties        self.age = age        self.name = namep = person("John", 21) #declaring an objectprint(p.name, p.age)

Output:

John 21

Conclusion:

This is just the beginning. Python is a very interesting programming language that you can use to develop your skills in programming and become a pioneer in your desired field of work. Happy Coding and all the best as you begin your Python journey!


Original Link: https://dev.to/codeschris/python-101-the-ultimate-python-tutorial-for-beginners-1fi2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To