Introduction
You most probably know why and are now scouring the internet for Zen advice on how to go about the language. Well, Same here.
We will discuss the absolute basics and I will link some resources(and cheatsheets) that will be be helpful in learning the Language. I will discuss Primitive Data Types, Math Operands and inbuilt data Structures.
Data Types
1. Strings
- Strings are usually alphanumeric characters enclosed in "" or ' '. They can be manipulated in a variety of ways through: Concatenation, Replication, Slicing and a variety of other methods(convert to upper, lower and replacing or iterating through the characters)
x = 'Python'
y = 'Developer'
#string Concatenation
x + y # returns PythonDeveloper
#string Replication
x * 3 # returns PythonPythonPython
len(x) # length of string -- in this case 6
x[0] #returns first letter - p
x[-1]#returns the last letter - n
x[0:3]#returns characters from within the specified range -- pyt
name = f 'I am a {x }{y}' # returns I am a python developer
x.upper() #converts to uppercase PYTHON
x.lower() #converts to uppercase python
x.title() #converts to titlecase Python
x.strip() #removes first and last character and returns the string
x.find(“p”) #finds index of passed character within the string
x.replace(“a”, “b”) #replaces the first passed character with the second one
“a” in x #find character within the string
2. Numeric - These values can wither be integers, floats or complex numbers
3. Boolean - These is either True or False. It can be used mainly for comparison operations and conditional statements.
4. Special- None
a = 1 # integer
b = 1.1 # float
c = 1 + 2j # complex number (a + bi)
d = “a” # string
e = True # boolean (True / False)
b = () #an empty list evaluates to None
Math Operands
- Mathematical Operands enable the performance of mathematical operations within our program.
- Just like ordinary mathematics, the operands appear in order of precedence as listed below: (*) ()(/)(//)(%) (+)(-) in that order.
2 ** 3 #returns the exponent = 8
2 * 3 # returns 6
12 / 2 #returns 4
24 // 5 #returns 4
24 % 5 #returns the remainder that is 4
28 + 3 #returns 31
28 - 3 #returns 25
Data Structures
1. Lists
- Lists contain a collection of items. They are very similar to arrays, however the only difference is that list can store different data type elements while arrays only store elements of a similar data type
- They are defined using [] or list() constructor
list1 = ['potatoes',59, 8.5, 'sugar']
list2 = list(('potatoes',59, 8.5, 'sugar'))
2. Tuples
- They are an immutable(hence they cannot be changed or modified once declared) and unordered data structures that store a collection of items. They are defined using ()
tuple1 = ('potatoes',59, 8.5, 'sugar')
len(tuple1) # returns 4 that is the number of items in the tuple
del tuple1 # to delete the entire tuple
Difference Between Tuples and Lists
- Lists are defined using [] while tuples are defined using ()
- Lists are mutable(can be changed and modified) while tuples are immutable
- Tuples are more memory efficient and execute faster than lists.
3. Dictionaries
- Dictionaries are data structures that store items as key:value pairs. They are defined using {}
- Using the keys and values different items within the dictionary can be accessed
my_dict = {3: 'd', 2: 'c', 1: 'b', 0: 'a'}
my_dict[0] # returns the first item in the dictionary
4. Sets
- Sets are unordered data structures that store unique elements_(No duplicates Allowed!!)_. They are defined using {}
my_set = {5, 4, 6, 7}
VARIABLES AND CONSTANTS
Variables are place-holders that serve to reference items. CONSTANTS are values that once declared remain the same and cannot be changed within the scope of the program.
Rules that Governs naming a variable.
- It can be only one word(No spaces between the words please)
- It can use only letters, numbers, and the underscore (_) character.
- It can’t start with a number. CONSTANTS are usually declared in uppercase letters.
Comments
Python comments are preceded with a #. For multiple lines, you can learn a shorthand command for commenting, eg. Ctrl + K + C in VSCode.
#this is a CONSTANT
CONSTANT = 5
#this are variables
myName = 'Developer X'
my_age = 15
Beginner Friendly CheatSheets
- Programming with Mosh : https://programmingwithmosh.com/python/python-3-cheat-sheet/
- Official Python Cheatsheet : https://www.pythoncheatsheet.org/
Top comments (0)