Sitemap
Divya Stephen

Divya Stephen

Software Developer l Academic Writer

Python Basics

5 min readSep 22, 2023

--

Python is one of the most popular programming languages in the world, created in the early 1990s.

Features

  • It has a simple, easy to learn syntax, which emphasizes readability.
  • Applications written in Python can run on almost any computer, including those running Windows, macOS, and popular distributions of Linux.
  • Furthermore, the ecosystem contains a rich set of development tools for writing, debugging, and publishing Python applications.
  • Python is an interpreted language, which reduces the edit-test-debug cycle because there’s no compilation step required.

Before starting the Codiiingggg, I would like to say that, Python is easy to learn, even if you are zerooo in coding, you can catch it up. Python syntax is so simple that you feel like you are writing a normal English sentence, but the only thing you need to keep in mind is the Proper Indentation. So for some features, you can see the examples, not the heavy sentences that explain to you what it is. Just go through the article once, twice, even thrice, and open the compiler, type your program. Happy Learning :)

Press enter or click to view image in full size

First Python Program

Guessed it right, its Hello World ! ;)

print("Hello World")  #can use single or double quotes

Variables and Data Types

  • Variables are one of the fundamental building blocks of programs.
  • It hold data in memory.
  • They have names, and they can be referenced by those names.
  • Variables also have types, which specify what type of data they can store (such as string and integer), and they can be used in expressions that use operators (such as + and -) to manipulate their values.
  • Variable names are case sensitive and can use any letter, number, and the underscore (_) character. However, they can’t start with a number.

In Python, a variable is declared and assigned a value using the assignment operator =. The variable is on the left-hand side of the operator, and the value being assigned is on right-hand side.

Note: Python is a dynamically typed language, so that the variable type is determined by the data assigned to it and no need of declaring the type of data.

x = 1      #here, we have variable x the value is 1
z = "Hey!" #variable z & the value is "Hey!"
a = True #a is the varibale which is a boolean data type & holds the value True


#To know the type of the variable
print(type(x))
print(type(z))

Python Operators

Operators are used to perform operations on variables and values. Python divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators
#Arithemetic Operations

x=5
y = x + 5 # assign variable y the value of x plus 5
print(y)
print(5//2) # // removes the decimal
print(5/2)
print(5%2) #% returns remiander
print(5**2)


#Assignmnet Operations
a=5 #here = is assignmnet operator
a+=10 #here += is assignmnet operator

#Reducing Operators
a=5
a=a+10 # and
a+=10 #are the same

print(a)


#Comparison Operations
print(3<4)
print(3>4)
print(3==4)
print(3!=4)

#Logical Operations
print(3>2 and 2<4)
print(x < 5 or x < 4)
print(not(x < 5 and x < 10))

Identity Operators: “is” and “is not”

  • is Returns True if both variables are the same object
  • is not Returns True if both variables are not the same object

Membership Operators

  • in Returns True if a sequence with the specified value is present in the object
  • not in Returns True if a sequence with the specified value is not present in the object

Bitwise Operators: are used to compare (binary) numbers: ( &,|, ~)

Get Divya Stephen’s stories in your inbox

Join Medium for free to get updates from this writer.

We can also concatenate the strings and convert one type to another data type.

#Concatenation
x = 'Hello' + ' ' + 'World!'
print(x)

#Type Converion
a= 67
print("Hello " + str(a))

To get today’s date, you can call the today() function:

from datetime import date
print(date.today())

To get information from the user (user input), you’ll use the input()

name = input("Enter your name: ")
print("Hello," + name)

Python Conditions and If statements

a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

For Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
for x in "banana":
print(x)

The Break Statement, with the break statement we can stop the loop before it has looped through all the items.

fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)

The Continue Statement, With the continue statement we can stop the current iteration of the loop, and continue with the next

fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)

The range() Function

  • To loop through a set of code a specified number of times, we can use the range() function,
  • The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
for x in range(6):
print(x)


for x in range(2, 6):
print(x)

While Loops

With the while loop we can execute a set of statements as long as a condition is true.

i = 1
while i < 6:
print(i)
i += 1

String

  • Its a collection of zero or more characters. Strings are commonly declared using single quotation marks, but you can also use double quotation marks.
  • Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.
  • However, Python does not have a character data type, a single character is simply a string with a length of 1.

We can perform various operation in string as follows,

a = "Hello, World!"
print(a[1])
print(len(a))
print("H" in a)

#Slicing
print(a[2:5])
print(a[:5])
print(a[2:])
print(a[-5:-2]) #Negative Indexing: to start the slice from the end of the string

#Modify Strings
print(a.upper())
print(a.lower())

print(a.strip()) #strip() method removes any whitespace from the beginning or the end
print(a.replace("H", "J")) #replace() method replaces a string with another string
print(a.split(",")) #split() method returns a list where the text between the specified separator becomes the list items
print(a.find('H')) #returns 0, if present and -1 if not present
print('H' in a) #Returns True if present, else False

String Concatenation, To concatenate, or combine, two strings you can use the + operator.

a = "Hello"
b = "World"
c = a + b
print(c)

String Format

We can combine strings and numbers by using the format() method, The format() method takes the passed arguments, formats them, and places them in the string where the placeholders {} are:

age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))

String Methods

Python has a set of built-in methods that you can use on strings.

Note: All string methods return new values. They do not change the original string.

For more detail

--

--

Written by Divya Stephen

Software Developer l Academic Writer