Python
  • Getting Started
    • Input and Output
    • Variables
    • Data Types
    • Conditional Structures
      • Humidity Level
      • Financial Transactions
      • Fruit-Vending Machine
      • Magic While
      • FizzBuzz
    • Functions
      • Hashtag Generator
  • Data Structures
    • Lists
    • Dictionary
    • Set
    • Interpreter Expressions
  • OOPs
    • Classes and Objects
    • Exception Handling
      • Simple Calculator
  • System Programming
    • File Operations
    • Directory Navigation
    • Process Creation
    • Threads
Powered by GitBook
On this page
  • Numbers
  • Numeric types
  • Strings and thier Operations
  • String Methods
  • Formatting in String
  • Truth value testing of String
  1. Getting Started

Data Types

I LOVE DOING THIS :)

Numbers

If you want to verify if an integer belongs to the class int you can use isinstance

You will need to pass number as the first argument and class name as the second argument, and it will check if the object is an instance of the class

>>> isinstance(2, int)
True

Numeric types

There are three numeric types in Python - int for integers, float for decimal numbers, and complex for complex numbers. General mathematical operations such as addition, subtraction, and multiplication can be done on them

Assign two complex numbers to two variables. say, complex_var and complex_var1 and print the sum of the two

>>> complex_var = (1 + 2j)
>>> complex_var1 = (2 + 3j)

>>> print(complex_var + complex_var1)
(3+5j)

Strings and thier Operations

We can assign a character ‘a’ to a variable single_quote_character. Note that the string is a single character and it is “enclosed” by single quotes

>>> single_quote_character = 'a'
>>> print(single_quote_character)
a
>>> print(type(single_quote_character)) 
<class 'str'>

Interestingly we can check the equivalence of one to the other using the keyword is, it returns True

>>> print(double_quote_multiple_characters is double_quote_multiple_characters)
True

String Methods

Get the index of a substring in a string.

>>> "abcde".index("c")
2

Test if a substring is a member of a larger string. This is done using the keyword in and writing the test

>>> "a" in "akash"
True

>>> "c" in "roger"
False

Join a list of strings using the join method. A list of strings is written by delimiting the sequence with a comma , and enclosing the whole group with brackets []

>>> combined_string = " ".join(["1", "2", "3"])
'1 2 3'

>>> combine = 'akash'
>>> combine = " ".join(combine)
'a k a s h'

Break a string based on some rule. This takes in the string as the object on which the method split is passed using the dot operator (.)

>>> "1 2 3".split() 
['1', '2', '3']

>>> param = "disallow:/cgi/bin"
>>> check = param.split(':') [::-2]
['/cgi/bin']

The syntax [::-1] can be used to reverse the elements in a list or other sequence, or we can even use reverse() method to reverse the elements

You can also use the slice notation to reverse just a portion of a list. For example, to reverse the middle three elements of the list numbers, you could use the slice notation numbers[1:-1][::-1]. This would give you the list [1, 5, 4, 3, 5]

Access individual characters in a string. Note the first element has index 0. You access the first element with the index 0, second element with the index 1, and so on

>>> param = "disallow:/cgi/bin"
>>> check = param.split(':')
['disallow','/cgi/bin']

>>> print(check[1])
/cgi/bin

Formatting in String

Just like C language we can use %s as a formatter which will enable you to insert different values into a string at runtime and thus format the string

>>> print("I love %s in %s" % ("programming","python"))
'I love programming in python'

An alternative way is to use the format keyword

>>> print("I love {programming} in {python}".format(programming="programming",python="python")
'I love programming in python'

Truth value testing of String

A string is considered to be true, if it's got some value in it or else it is considered to be false

>>> print(bool(""))
False

>>> print(bool("G"))
True
PreviousVariablesNextConditional Structures

Last updated 2 years ago