DEV Community

Cover image for Python 101: Introduction to Python Language for Beginners
shawnkiplagat69
shawnkiplagat69

Posted on

Python 101: Introduction to Python Language for Beginners

Guido van Rossum designed Python, an interpreted high-level language that was launched in 1991.

  • Python programs end in.py and can be executed by running python file name.py from the command line.

  • The usage of white space rather than the more common symbols to delimit code blocks is probably its most obvious feature.

  • End-of-line semicolons (;) are optional in Python and are rarely used.
    Python has emerged as the finest answer in a variety of fields, including online applications, data analysis, data science, machine learning, and artificial intelligence.

Python's Most Common Features:

• Simplicity: Focus on the code rather than the language's syntax.

• Open Source: A powerful language that anybody can use and modify as they see fit.
• Portability: Python code can be shared and will continue to function as intended.
• Python is embeddable and extensible, which means it can contain snippets of other languages to execute certain tasks.
• Being Interpreted: Python takes care of huge memory tasks and other CPU-intensive operations, letting you to concentrate just on coding.
• Object Orientation: Objects aid in the decomposition of complex real-world problems so that they may be coded and solved to produce solutions.

Python's applications include:

  1. Web Development - Python is used in the development of websites using popular frameworks such as Django, Flask, and FastApi.
  2. Embedded Applications - Because Python is based on C, it may be used to generate embedded C software for devices like the Raspberry Pi.
  3. Game development - the process of creating interactive games with the help of popular libraries such as pygame.
  4. Data Science and Data Visualization - Using data libraries like Matplotlib and Seaborn, data may be manipulated, represented, analyzed, and visualized. Graphical User Interface Applications (GUI) - Tkinter is used to create user interfaces.
  5. Machine Learning and Artificial Intelligence - Computers learn algorithms based on stored data using libraries such as Pandas, Scikit-Learn, and NumPy.

Python Installation

Download the latest Python version here based on your operating system

Pycharm installation

Pycharm is a popular IDE(Integrated Development Environment) used for writing ,debugging and running python scripts.
Download Pycharm IDE to start coding

Visual studio code installation

Visual Studio Code is a Text-editor used to write and run python scripts. Download Visual Studio Code and start coding!

Building your first Python Program

"Hello World"

Start your Pycharm IDE, select a new file and save it with the .py extension. Example hello_world.py.

Write the following code in the hello_world.py file and run your first program!

print("Hello World")
Enter fullscreen mode Exit fullscreen mode

The Print() function is an in-built function used to give outputs.

The following Output will be displayed:

Hello World
Enter fullscreen mode Exit fullscreen mode

Variables

Variables just like hotel suites, are containers for storing data values.
Here's the syntax for assigning variables to data values.

variable_name = data_value
 For example, name = shawn
Enter fullscreen mode Exit fullscreen mode

Rules for Naming Variables

  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
  • Variable names are case-sensitive (name, Name and NAME are all different variables). Reminder though: Variables need not to be declared. Their data types are inferred from their assignment statement, meaning that different data types can be assigned to the same variable throughout the code. This is just what makes Python a dynamically typed language.

Keywords

Every programming language has special reserved words, or keywords, that have specific meanings and restrictions around how they should be used. Python is no different. Python keywords are the fundamental building blocks of any Python program. Examples of such keywords include False, True, and, is, class, def, return, try, as ,pass, break, continue, for, while and except.

Identifiers
Identifiers are names used to identify variables, functions,
classes, modules or other objects. Identifiers start with letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).

Literals
Literals are the raw data that is assigned to variables or constants while programming in Python. String literals, numeric literals, boolean literals, literal collections, and a special literal, None, are the five types of literals we have. 

Data Types
A Data Type is a classification that specifies which type of value a variable has and of what type. Examples of in-built data types include

  1. Binary Types: memoryview, bytearray, bytes.
  2. Boolean Type: bool.
  3. Set Types: frozenset, set.
  4. Mapping Type: dict.
  5. Sequence Types: range, tuple, list.
  6. Numeric Types: complex, float, int.
  7. Text Type: str.

Comments

Comments in Python are the lines in the code that are ignored by the interpreter during the execution of the program. Generally comments enhance the readability of the code and help other programmers interpret your code.
Types of comments include:

  • Single line comments
#This is a single line comment
Enter fullscreen mode Exit fullscreen mode
  • Multi-line comments
'''
 This 
 is a multi-line 
 comment
'''
Enter fullscreen mode Exit fullscreen mode

Operators

Operators are special symbols in python that carry out arithmetic and logical computation. The value that the operator operates on is called an Operand.

Types of Operators include

1. Arithmetic Operators

print(2 + 3)  # 5 (addition)
print(3 - 2)  # 1 (subtraction)
print(3 * 3)  # 9 (multiplication)
print(84 / 2)  # 42.0 (division)
print(2 ** 2)  # 4 (exponent)
print(3 % 2)  # 1 (remainder of the division)
print(11 // 2)  # 5 (floor division)
Enter fullscreen mode Exit fullscreen mode

2. Comparison Operators

==  Equal to    2 == 3
!=  Not equal   2 != 3  
>   Greater than    3 > 2   
<   Less than   2 < 3   
=>  Greater than or equal to    x => y
<=  Less than or equal to    x<=y  
Enter fullscreen mode Exit fullscreen mode

3. Assignment Operators

=      x = 5   same as   x = 5  
+=    x += 3   same as   x = x + 3  
-=    x -= 3   same as   x = x - 3  
*=    x *= 3   same as   x = x * 3  
/=    x /= 3   same as   x = x / 3  
%=    x %= 3   same as   x = x % 3  
//=   x //= 3  same as   x = x // 3 
**=   x **= 3  same as   x = x ** 3
Enter fullscreen mode Exit fullscreen mode

4. Logical Operators

and     Returns True if both statements are true    x < 5 and  x < 10   
or  Returns True if one of the statements is true   x < 5 or x < 4  
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)
Enter fullscreen mode Exit fullscreen mode

5. Membership Operators

in   Returns True if a sequence with the specified value is present in the object    x in y  
not in  Returns True if a sequence with the specified value is not present in the object    x not in y
Enter fullscreen mode Exit fullscreen mode

Conditions and If Statements

Python statements such as if , elif and else are used when you need python to check whether a certain condition is met before a certain line of code gets executed.

#if-else ladder used in grading a students results
if grade >= 85:
  print("Grade A ")
elif grade >= 80:
  print("Grade B ")
elif grade >=70:
  print("Grade C ")
elif grade >=60:
  print("Grade D ")
else:
  print(" Sorry, you have failed terribly!! Repeat the semester")
Enter fullscreen mode Exit fullscreen mode

Loops

A Loop also known as an iterative statement, is a control flow statement that is used to repeatedly execute a group of statements as long as a certain set condition is satisfied.

Python has two primitive loop commands which are while and
for loops.

- For Loop - It Iterates through the elements(list, tuple, dictionary, set, string)in the order that they appear.

#this function prints each name in the name list:
names = ["shawn", "victor", "joy","anthony"]
for all_names in names:
  print(all_names)
Enter fullscreen mode Exit fullscreen mode

- While Loop - executes a set of statements as long as a condition is true.

#functoin that prints i as long as i is less than 6:
i = 1
while i < 6:
  print(i)
  i += 1
Enter fullscreen mode Exit fullscreen mode

- The Break Statement
It is used to exit a loop when an external condition is triggered.

#Exits the loop whenever the value of i is equal to 3
i = 1
while i < 6:
  print(i)
  if i == 3:
    break
  i += 1
Enter fullscreen mode Exit fullscreen mode

- Continue Statement
It is used to skip the execution of the current iteration and continues to the next.

#Continues to the next iteration if i is 3:
i = 0
while i < 6:
  i += 1
  if i == 3:
    continue
  print(i)
Enter fullscreen mode Exit fullscreen mode

- Pass Statement
When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed.
Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

def info():
print("This function will get executed while the second function won't be executed")
 info()


def myfunction():
  pass
#having an empty function definition like this, would raise an error without the pass statement
Enter fullscreen mode Exit fullscreen mode

Functions

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

How to create a function in Python

Use the def keyword to define a function
example

def sum():
print(2+3)

#Calling the sum function
sum()
#The output is 5
Enter fullscreen mode Exit fullscreen mode

Arguments and Parameters

Data can be passed to functions through parameters inform of arguments.
Parameters are specified after the function name, inside the parentheses and are separated with a comma.
An Argument is the actual data passed to the function.

def sum(a,b):
'''a and b are the parameters  
sum is the function
def is used to define the function
'''
print(a+b)


#Calling the function
sum(2,3)
#2 and 3 are the arguments
#The output is 5
Enter fullscreen mode Exit fullscreen mode

Well I guess that's all that we shall be covering in this article. I am very delighted that you had the chance to go through it. Do stay tuned though, for more Python Concepts articles so that we all learn together.

Top comments (0)