Learning Python Smart Way : Numbers & Strings
In this series of blogs we will learn python , But in a smart way, I mean by smart way learning python learning deep with basics and learn only needful things that we are going to implement in future project and in more practical manner.
Here I discussed about Installing Python and Starting IDLE . Lets start with Numbers and Strings.
Contents
Numbers :-
We will learn about following topics :-
1. Type of Number in Python.
2. Arithmetic operations.
3. Assignment of Object
Type of Number in Python :-
In python , We have lots of type of numbers, I will mainly discuss about Integers and Floating point numbers.
Integer example is 1. –2 etc and Floating point number is 3.01 etc.
Basic Operations example :
Arithmetic operations:-
Note : In python 2, If you divide 3/2 you will get the integer by in python 3 you will get the float value.
In you have python 2 installed , You can import division module from “__future__” to get the Floating point number.
Assignment of Object:-
In python , Lets create some variables, In python we can assign any data type to a variables that why I love python most.
Using print(“ ”) function to print values e.g variables in the below image.
The names you use when creating these labels need to follow a few rules:
-
Names can not start with a number.
-
There can be no spaces in the name, use _ instead.
-
Can’t use any of these symbols :'”,<>/?|\()!@#$%^&*~-+.
-
It’s considered best practice (PEP8) that the names are lowercase.
Strings :-
Lets learn following topics in strings.
- Creating & Printing Strings
- String Indexing and Slicing
- String Properties & Methods
- Formatting Print
Creating & Printing Strings
String Indexing and Slicing
We can use a : to perform slicing which grabs everything up to a designated point.
Note : The slicing Here we’re telling Python to grab everything from 1 up to 2. It doesn’t include the 2rd index. You’ll notice this a lot in Python, where statements and are usually in the context of “up to, but not including”.
We can also use index and slice notation to grab elements of a sequence by a specified step size (the default is 1). For instance we can use two colons in a row and then a number specifying the frequency to grab elements. For example:
# Grab everything, but go in steps size of 1.
# Grab everything, but go in steps size of 2
# We can use this to print a string backwards by (-1)
String Properties & Methods
Its important to note that strings have an important property known as immutability.
s = ‘Hello World’
# Let’s try to change the first letter to ‘x’
s[0] = ‘x’
Formatting Print
That’s all folks , In next series we will learn about List, Dictionaries and Tuples.