DEV Community

Victor108003
Victor108003

Posted on

Python 101:The ultimate Python Tutorial

Introduction
Python is a high level programming language and an interpreted programming language.

(#)Printing hello world.
print('Hello world')
output:Hello world
The extension of python is (.py)
Comments
They explain how a code works.

Unlike other programming language python uses whitespace and indentation to construct the code structure.

Keywords
Are words that have special meaning in python.
To find current keywords you use;
import keyword
print(keywords.kwlist)

Python uses(''),(""),("""),(''')to denote a string literal.

Variables
Used to store values.

`message='Hello world'
print(message)

message='Good bye'
print(message) #message is a variable

output:
Hello world
Good bye`

To define a variable you use the following syntax:
variable_name=value

Guidelines to define variables
(i)Variables name should be concise and descriptive
(ii)Use underscore to separate multiple words.
(iii)Avoid using letter I and uppercase O cause they look like numbers.

Strings
In python anything inside quotes is a string.
eg:
name='Ashly'

Control flow

a.if statement
syntax:

if condition:
if block
eg.
age=input("Enter you age:")
if int(age>18):
print("You are an adult")

b.if...else statement

performs an action when condition is true and another action when condition is false.
syntax:
if condition:
if block;
else:
else block;

c.for loop

syntax:     
    for index in range(n):
        statements;
Enter fullscreen mode Exit fullscreen mode

use the range(start,stop,step) to customize loop.

d.while loop

Executes statement when condition is true.
syntax:

while condition:
body;

Use break to terminate loop in while or for loop

lists
Is an ordered collection of items.
Python uses[]to indicate a list and to separate items you use comma(,).
Tuples
Is a list that cannot change.
Uses parentheses().

append is used to add elements to the existing list.
insert adds new element at any position.
del allow you to remove element from a list.**
pop removes last element from a list.

Top comments (0)