Intro into Python- Basic operations
Why Python
The python language is one of the easiest programming languages available to learn because of its simplified syntax(uses English-based keywords) and that’s why it best to learn python than any other programming language.
Uses of Python
Another Reason that python is such a great language to learn is because of its usage in:
- Machine Learning
- Data Science
- Web Development
- Web Scraping
- Game development
- Gui Development
Getting into the Basics
Variables
Variables are the first thing you learn in any programming language. So what is a variable?
Python variables can be thought of as a bucket or a container that holds some values. For Example,
let’s give a variable called ‘var’ some value.
Here we are giving a defining pointer called ‘var’, that points to some data called ‘I am a variable’.
A variable can point to various data types and mainly there are 4 types of data types:
- String
Here the data type is a string and the string is enclosed in quotes. Python accepts single quotes'
double quotes"
and triple quotes"""
to denote the literal strings.
- Numerical
When the data type is numeric like 1,2,3,4,5
- Booleans
After evaluating an expression, python returns an expression that returns either true or false.
Float
Number with a decimal point
Print statements
The print function some string on the screen. If the text is not a string it is converted into a string before it is written to the screen.
Conditional statements
To make a decision when a certain condition is satisfied, if,elif, else statements are used. When an expression is true, it passes through the ‘if’ block and if it does not pass, it goes with the else case. Elif is used to check for multiple expressions. If the ‘if’ block is not true, it goes with the elif block. If the elif block is true, it goes with that one otherwise it goes with the else block.
syntax
- There should be a colon after the statement
- There should be a tab space between the block that is true
- You cant use elif after else block or before if block.
String Slicing
The string data type is a very important data type in python. Now after getting to know a little about string data type let’s discuss string slicing and its methods
So before starting, Let’s know a little about indexing in python:
Syntax
string[start:stop:step]
The start argument tells it the starting index(default zero) from its should break the string. The stop argument tells it the last index(its length) till where it should break the string. The step tells it how many characters it should leave(default 1) to return the new sliced list. Let’s see it in code:
Now if you don’t know the string length, you can get it by a python’s built-In-Function called ‘len()’. So if want to get a len of any data type len function can help. A basic example of len function👇👇:
length = "Hello"
print(len(length)) # Prints 5
If you want to get the sentence length using the length function👇👇:
As told above, if you add a second colon in between these brackets(‘[]‘) it tells the program to skip how many characters to miss. So here is an example:
string = 'Hello all!!!'
print(string[0:len(string):2]) # print 'Hloal!'
And using this, there is a very useful trick to print a sentence backward by putting -1 in the third argument:
string = 'Hello all!!!'
print(string[len(string)-1::-1]) # print '!!!lla olleH'
Now let’s get into :
String Functions/Methods in Python
Python is provided with some built-in methods that you can use on strings. Some of these methods are:
- capitalize: Makes the first character to upper case
- isupper: Returns True if all characters in the string are upper case
- endswith: Returns true or false if the string ends with the specified value
- startswith: Returns true or false if the string starts with the specified value
- title: Converts the first character of each word to upper case
- isdigit: Returns True/False if all characters in the string are digits
- casefold:Converts string into lower case
- strip:Remove spaces at the beginning and at the end of the string
- isalpha: Returns True/False if all the characters are alphabet letters and doesn’t include !#(space)? etc
These are some of the very commonly used methods. All of these methods with explanation can be found at w3schools.
For the next bit, I would recommend learning Loops And Ranges
Thank You
This post was originally published at https://www.decodebuzzing.com