Posts

Showing posts from October, 2019

comparison operations in python

Image
there are 8 types comparison operations in python x < y x <= y x > y x >= y x == y x != y x is y x is not y < operation will return true if y is strictly greater than x. otherwise false. <= operation will return true if y is greater than x or equal to x. otherwise false. > operation will return true if y is strictly less than x. otherwise false. >= operation will return true if y is less than x or equal to x. otherwise false. and when you try to use above 4 operations, x and y have to be same data type. otherwise, TypeError will be raised. == operation will return true if x and y are same that means the value of them will be evaluated not object itself. != operation will return true if x and y are not same. otherwise, false. is operation will return true if x and y are same object. otherwise, false. is not operation will return true if x and y are not same object. otherwise false. I will show you example code to help you understand easier

python logical operators

Image
there are three logical operators in python. if you have some experience about other programming languages, you might be familiar with ||, && and ! operation. but python has logical operations like below x and y x or y not x I think it looks more readable. and operation will return true if both x and y are true. otherwise false. or operation will return true if either x or y is true. if both of them are false, it will return false. lastly, not operation will reverse x. if x is false, it will be true. otherwise false. we can understand easier from example code. x = True y = False print ( "x is true and y is false then : {}" . format(x and y)) y = True print ( "x is true and y is true then : {}" . format(x and y)) print ( "x is true or y is true then : {}" . format(x or y)) x = False print ( "x is false or y is true then : {}" . format(x or y)) print ( "x is false and then not x : {}" . format(

how to strip string in python

Image
there is strip function in python. strip function strips string literally. there are 3 types of strip. str.rstrip([chars]) str.lstrip([chars]) str.strip([chars]) the way how to use is all the same but the direction which side will be stripped is different. chars parameter is characters to be stripped. if none, the default value is whitespace. I made example code. this will be helpful to understand the operation. str1 = " hello python " print (str1 . rstrip()) print (str1 . lstrip()) print (str1 . strip()) str2 = "aaaaaaahello pythonbbbbbbbb" print (str2 . lstrip( "a" )) print (str2 . rstrip( "b" )) print (str2 . strip( "ab" ))

isalpha, isdigit and isalnum functions in python

Image
we can check whether any string is consist of alphabet or numeric or both in python. let's learn each function one by one str.isalpha() this function will return true if all characters in the string are alphabet. otherwise, return false. str.isdigit() this function will return true if all characters in the string are digit. otherwise, return false. str.isalnum() this function is combined with isdigit and isalpha. if all characters are digit number or alphabet, this function will return true. otherwise return false. you can learn them well from code. str1 = "python" str2 = "python " str3 = "123" str4 = "python3" str5 = "python3.7" print ( '"python" -> isalpha: {}, isdigit: {}, isalnum: {}' . format(str1 . isalpha(), str1 . isdigit(), str1 . isalnum())) print ( '"python " -> isalpha: {}, isdigit: {}, isalnum: {}' . format(str2 . isalpha(), str2 . isdigit(), str2 . is

string slicing in python.

Image
sequence types can be sliced in python. str is also immutable sequence type that means str objects can be sliced. so, I will introduce how to slice string in this page. I think slicing is very good tool in python. so, if you are familiar with this, it will be very helpful in the future. there are 3 types to slice string like below. s[i] s[i:j] s[i:j:k] i and j are index of string. k is the step. when you set only i, you can get ith character of string. and if you set i and j, you can get sub-string from i to j. if you set i, j and k, you can get sub-string from i to j with k step. i is included in the range. and j is not included in the range. the index can be negative value. for example, -1 means last one of string. -2 means second last one of string. let's check them in code. strSource = "hello python" print (strSource[ 0 ]) print (strSource[ 0 : 5 ]) print (strSource[ 0 : 12 : 2 ]) print () print (strSource[ - 1 ]) print (strSource[ 6 :

string index function in python

Image
the operation of this function is very similar to find function. the just one difference is that if sub string is not found by index function, ValueError exception is raised but find function will return -1. if you wonder about find function, refer to  how to use find function of str in python in this page, I will introduce index function. the declaration for index is like below. str.index(sub[, start[, end]]) it is almost same to find function excepting for the function name. the way how to use this function is also almost same. sub parameter is what you want to find from "str". and start and end are the range of str. it will be from 0 to the length of string. the default value of start is 0 and end is the length of string. let's check it in example. 1 2 3 4 5 6 7 srcString  =   "hello python"   print (list(enumerate(srcString))) print ( "length of string : {}" . format ( len (srcString)))   print (

how to use find function of str in python

Image
we can find the index of string that we want to find from the source string with str.find function. str.find(sub[, start[, end]]) the declaration is like above. sub is what you want to find. and you can also define the range from which the sub-string will be found. for example, there is "hello python". and each character has the index. the index begin from 0. as a result, 'h' will be index 0. and 'e' will be 1. and so on... return to the this page's subject, if you set start parameter to 6, and end to 12, the sub will be searched from index 6 to 12. further more, if you don't set start parameter, the default will be 0. and if you don't set end parameter, the default will be length of string. if sub is not found, the return value will be -1. let's check them in example code. 1 2 3 4 5 6 7 8 srcString  =   "hello python"   print (list(enumerate(srcString))) print ( "length of string :

join strings with separator in python

Image
how can we join multiple string with any separator in python? actually, we need such this operation when we are programming. so, I will introduce how to join strings with separator in this page. there is a nice method in built-in type "str" in python. it is str.join(iterable) in order to join strings, you should make iterable that has strings witch you want to join with separator. but every element of iterable must be str type. otherwise, TypeError Exception will be raised. and "str" part will be separator string you want. I think it is the best way to understand something to see it. let's see it. 1 2 3 4 5 sep  =   " " strings  =  ( "my" , "name" , "is" , "simpp" )   print (strings) print (sep.join(strings)) cs

split string in python

Image
it's very easy to split string by any separator in python. I will introduce the way to split string in python. str that is built-in type in python has split method. str.split(sep=None, maxsplit=-1) you can pass any separator to sep parameter key. it can be also string like "<>". and there is another one for parameter, maxsplit. you can define how many string will be splited. the default value is -1 that means string will be splited at most. but if you set the parameter to any value, the string will be splited at most that value. for example, if you set the value to 2, the number of elements will be 3 at most as the result of operation. let's check them. 1 2 3 4 5 6 7 sampleString  =   "a,b,c,d,e" sampleString2  =   "1<>2<>3<>4<>5" print (sampleString.split( "," )) print (sampleString.split( "," , 2 )) print (sampleString2.split( "<>"

python string literal declaration

Image
There are various ways to declare string literal in python. I will introduce them one by one in this page. First way is to use single quotes like 'string' 1 str1  =   'hello' cs Second way is to use double quotes like "string" 1 str1  =   "hello" cs Last way is to use triple quotes like """hello""" or '''hello''' 1 str1  =   '' 'hello' '' cs or 1 str1  =   "" "hello" "" cs Every way can declare string literal in python. but there are some differences. Triple quotes can declare multiple lines. all whitespace that is in triple quotes will be included in the string literal. Double quotes can include single quote in string and in contrary, single quotes can include double quote in string. so then, let's check them. 1 2 3 4 5 6 7 8 9 str1  =   "

python string format function

Image
we can use the formatted string with format() function in python. the declaration is like below str.format(*args, **kwargs) let's check how to use the function in the example. in order to check it, I typed codes like below. 1 2 3 4 5 6 7 print ( "my favorite fruit is {}" . format ( "apple" ))   print ( "{}'s favorite fruit is {}" . format ( "apple" ,  "he" ))   print ( "{1}'s favorite fruit is {0}" . format ( "apple" ,  "he" ))   print ( "{human}'s favorite fruit is {fruit}" . format ( * * { "human" :  "she" ,  "fruit" :  "banana" })) cs you can learn something from above lines. we can set the format filed with braces {}. each argument has position and the position is used as index in braces {} dictionary can be argument and the key is used in each format filed.  it's time to chec

declaration of variables in Python

Image
python is dynamic programming language. so, it is very simple and easy to declare variable in Python. you have to remember and type data types in static programming language such as C, C++ and JAVA. but it will be done with typing the name of variable in Python. let's try to declare variables. 1 2 3 4 5 6 7 8 9 10 number  =   1 string  =   "hello" mList  =  [ 1 , 2 , 3 , 4 ] mDict  =  { "name" :  "jack" ,  "age" :  25 }   print (number) print (string) print (mList) print (mDict)   Colored by Color Scripter cs I typed various typed variables such as string, number, list and dictionary like above. the result is like above. as you can see, in order to use variable in python, just type the variable name and assign any data to the variable.

how to pass arguments to python script

Image
if you want to pass arguments to script, you will be able to do it with this page. python script can get arguments from interpreter. but, in order to do it, you need built-in module "sys" "sys" has "argv" attribute. so, interpreter pass the arguments to "argv" attribute of "sys". let's try it. in order to get arguments, I created a script "main.py". and I coded like below. 1 2 3 4 import  sys print (sys.argv) cs as you can see, I imported "sys" and I printed out "argv" of "sys". in detail, "argv" is list data type. and in order to use attribute of any module, dot(.) has to be used between module name and attribute name. and then, I executed the script with some arguments. as you see above screen, the first argument is file name. and arguments the are passed from interpreter are begining from the second of list.

how to execute python script

Image
if you installed python, you can execute python script. I will introduce how to execute python script in page. first of all, we have to make python script to be executed. every file of which extension is ".py" can be python script file. so, I create "main.py" from notepad like below. if there is python script, we can execute it easily. first way to execute script, just double click like executing other programs. it will run if .py extension is associated with python. but, you can't see any result. because python interpreter will close the window when script is done. therefore, if you want see anything from that, you have to execute script from window command. this is second way. at first, open Windows command. then you can see below screen. and then, I moved to the location in which python script is. >cd (path) and then, type like below. >python main.py we can see "hello python"

how to check python version on windows

Image
in order to check python version, you have to open the command on windows. if you open command, type like below >>>python -v if you type "python -v", you can see more information about the build. or >>>python --version if you type "python --version", you can see simple version information like above screen.

let's try to install python on windows 10

Image
let's try to install python on windows 10 first of all, we must download installation file from official python homepage . if you click the link, you can see below screen. click downloads menu. when I see the homepage, latest version is 3.8.0 after downloading the installation file, execute that file. and then, you can see below screen first. Install Now -> default settings. if you choose this option, installation will start. Customize Installation -> you can change the location that python will be installed in. and you can change installation options. recommendation : check "Add Python 3.8 to PATH". if you check this, Python can search for modules that you installed. if you choose "Install Now", installation process is done. but, if you choose "Customize installation", you will see below screen. Documentation ->if you check this, python documentation will be installed as well. pip ->this is recommended.