Python Basics: Python Beginner Guide

python book
Photo by Christina Morillo on Pexels.com

In this article, learn Python programming basics with this comprehensive guide. Discover the key features of Python, including its simplicity, readability, and ease of use. Get started with Python’s syntax and learn how to write your first programs. Explore Python’s vast standard library and learn how to use it to perform a wide range of tasks. Whether you’re a novice or an experienced programmer, this guide will help you master the fundamentals of Python programming.

Also, let’s take a look at Python Basics

Table of Contents

Introduction

Python is one of the most popular programming languages in the world today. It has become increasingly popular in recent years, thanks to its simplicity, ease of use, and versatility. Python is used in various applications, including web development, data analysis, machine learning, scientific computing, and artificial intelligence.

Python’s versatility also makes it an excellent language to learn, as it can be used in many different areas. For example, if you’re interested in web development, Python can be used with popular frameworks like Django or Flask. If you’re interested in data analysis, Python has powerful libraries like Pandas and NumPy that make it easy to work with large datasets.

In today’s world, where data is king, Python has become a critical tool for businesses and organizations of all sizes. Companies like Google, Amazon, and Facebook use Python for a wide range of applications, from building web applications to analyzing data to training machine learning models.

Overall, Python is an excellent language for novice programmers to learn. Its simplicity, versatility, and widespread use make it a valuable skill to have in today’s world.

Basics

				
					#Hello World

print("Hello World")
#output: Hello World

				
			
				
					#Indentation: Correct way

if True:
    print("True")
else:
    print("False")

#output: True

				
			
				
					#Indentation: Improper

if True:
    print("Return Answer")
    print("True")
else:
    print("Return Answer")
print("False")

#output: 
Return Answer
True
False
				
			
				
					#Multiline

one = 1
two = 2
three = 3
total = one + \
        two + \
        three
print("Total:", total)

#output: Total: 6
				
			
				
					one = "I "
two = "like to "
three = "learn programming."
sentence = one + \
        two + \
        three
print("Sentence:", sentence)

#output: Sentence: I like to learn programming.

				
			
				
					#Array elements declaration

Week_days = ["Mon", "Tues", "Wed", "Thu", "Fri", "Sat", "Sun"]
print(Week_days)
print("1st weekday is: ",Week_days[0])

#output:

['Mon', 'Tues', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
1st weekday is:  Mon

				
			
				
					#Multiple commands in one line

p=4; q=5; r=6; s = p+q+r; print("Total:", s)

#output: Total: 15
				
			
				
					#Multiple values to multiple variables

p,q,r = 1,2,3 
print("p:q:r:", p,q,r)

#output:
p:q:r: 1 2 3
				
			

Strings Lists and Tuples and Dictionaries

				
					#string

str = "Hello World"

print(str)                       #prints complete string
print(str[0])			         #prints first character of the string
print(str[1:6])                  #prints characters starting from 1st to 5th  
print(str[3:])			         #prints string starting from 3rd character 
print(str[:])			         #prints the whole character
print(str[:6])			         #prints string from starting to 5th character	
print(str*2)			         #prints string two times
print(str + " Welcome!")         #prints concatenated string


#output: 

Hello World
H
ello 
lo World
Hello World
Hello 
Hello WorldHello World
Hello World Welcome!

				
			
				
					#Lists

list1 = ['abcd', 4321, 'ab', 41.3, 11]
list2 = [4321, 'abcd']

print(list1)
print(list1[0])
print(list1[1:3])
print(list1[2:])
print(list1*2)
print(list1 + list2)

list1[3] = 85
print(list1)

#output: 

['abcd', 4321, 'ab', 41.3, 11]
abcd
[4321, 'ab']
['ab', 41.3, 11]
['abcd', 4321, 'ab', 41.3, 11, 'abcd', 4321, 'ab', 41.3, 11]
['abcd', 4321, 'ab', 41.3, 11, 4321, 'abcd']
['abcd', 4321, 'ab', 85, 11]

				
			
				
					#Tuple

tuple1 = ('abcd', 4321, 'ab', 41.3, 11)
tuple2 = (4321, 'abcd')

print(tuple1)
print(tuple1[0])
print(tuple1[1:3])
print(tuple1[2:])
print(tuple1*2)
print(tuple1 + list2)

#output:

('abcd', 4321, 'ab', 41.3, 11)
abcd
(4321, 'ab')
('ab', 41.3, 11)
('abcd', 4321, 'ab', 41.3, 11, 'abcd', 4321, 'ab', 41.3, 11)
('abcd', 4321, 'ab', 41.3, 11, 4321, 'abcd')

tuple1[3] = 10
print(tuple1)

#output: 

TypeError: 'tuple' object does not support item assignment

				
			
				
					#Dictionary

dict1 = {}
dict1['one'] = "1"
dict1["three"] = "2"
dict1["three"] = "3"

dict2 = {"Name" : "John", "Gender" : "M", "City" : "NY"}

print(dict1['one'])
print(dict1["three"])
print(dict1)
print(dict2.keys())
print(dict2.values())

#output:

1
3
{'one': '1', 'three': '3'}
dict_keys(['Name', 'Gender', 'City'])
dict_values(['John', 'M', 'NY'])

				
			

Basic Operator

				
					a = 1
b = 2
c = 3
d = 0
e = 5
f = 6

d = a+b #addition
print("a+b = ", d)

d = a-b #subtraction
print("a-b = ", d)

d = a*b #multiplication
print("a*b = ", d)

d = a/b #division
print("a/b = ", d)

d = a//b #floor-division
print("a//b = ", d)

d = a%b #modulo-division
print("a%b = ", d)

d = b**c #sum
print("b**c = ", d)

#output:

a+b =  3
a-b =  -1
a*b =  2
a/b =  0.5
a//b =  0
a%b =  1
b**c =  8

				
			

Comparison Operators

				
					a = 1
b = 2
c = 3

#equality check
if (a==b):
    print("a is equal to b")
else:
    print("a is not equal to b")

#output: a is not equal to b
				
			
				
					if (a!=b):
    print("a is not equal to b")
else:
    print("a is equal to b")

#output: a is not equal to b
				
			
				
					a = 1
b = 2
c = 3


if (a<b):
    print("a is less than b")
else:
    print("a is greater than b")

#output: a is less than b
				
			
				
					a = 1
b = 2
c = 3


if (a<b):
D("a is less than b")
else:
    print("a is greater than b")

#output: a is less than b
				
			
				
					a = 1
b = 2
c = 3

if (a<=b):
    print("a is either less than or equal to b")
else:
    print("a is neither less than nor equal to b")

#output: a is either less than or equal to b
				
			
				
					if (a>=b):
    print("a is either greater than or equal to b")
else:
    print("a is neither greater than nor equal to b")

#output: a is neither greater than nor equal to b

				
			

Assignment Operators

				
					a = 1
b = 2
c = 0

c = a+b
print("a+b:", c)

c+=a
print("c+a:", c)

c*=a
print("c*a:", c)

c/=a
print("c/a:", c)

#output:

a+b: 3
c+a: 4
c*a: 4
c/a: 4.0


c = 2
print("c:", c)

c**=a
print("c**a:", c)

c//=a
print("c//a:", c)


#output:

c: 2
c**a: 2
c//a: 2
				
			

Logical Operators

				
					a = 1
b = 2

#and
if (a and b):
    print("Both a and b are not equal")
else:
    print("Both a and b are not equal")

#output: 
Both a and b are not equal


a = 0
b = 0

#or
if (a or b):
    print("at least one of 'a' or 'b' are not equal")
else:
    print("at least one of 'a' or 'b' are equal")

#output:
at least one of 'a' or 'b' are equal


a = 3
b = 7

#not
if not (a > b):
    print("a is <=b")
else:
    print("a is > b")


#output:
a is <=b

				
			

Membership Operator

				
					a = 0
b = 2
lsit1 = [1, 2, 3, 4, 5]

if (a in list1):
    print("a is in the list")
else:
    print("a is not in the list")

#output:
a is not in the list


if (b not in list1):
    print("b is not in the list")
else:
    print("b is in the list")
 

#output:
b is in the list
				
			

Conclusion

Python is a high-level, interpreted programming language widely used for various purposes, including web development, data analysis, machine learning, scientific computing, and artificial intelligence. Python is known for its simplicity, readability, and ease of use, making it a popular choice among beginners and professionals alike.

Python has a vast standard library that provides many built-in modules and functions, making it easy to perform a wide range of tasks without writing much code. It also supports object-oriented, functional, and procedural programming paradigms, which makes it a versatile language.

Python code is easy to learn and read, as it uses indentation to define code blocks, rather than curly braces or keywords like “end.” This makes it more readable and reduces the likelihood of syntax errors.

Python supports dynamic typing, which means that you do not need to specify the data type of a variable explicitly. Instead, Python infers the data type automatically based on the value assigned to the variable. This feature makes Python code more concise and readable.

Overall, Python is a powerful and flexible language that is ideal for beginners and professionals alike. Its ease of use, readability, and vast libraries make it a popular choice for various applications.

Recent Articles