Python is the a very simple but robust programming language.
Versions: python 2 and 3 are the latest verions in use
Tools: python anywhere, jupyter notebook, anaconda etc can be used to practice the python
Data Types:-
>>> int1 = 5
>>> print(int1)
5
>>> float1 = 7.0
>>> print(float1)
7.0
>>> string1 = ‘String Variable’
>>> print(string1)
String Variable
>>> print(stringdoublq)
we can use apostrophes with double quote, like, it today’s lessonSingle Quote Drawback with special characters
>>> stringsingleq = ‘today’s lesson’
File “”, line 1
stringsingleq = ‘today’s lesson’ ^
SyntaxError: invalid syntax
>>>
More combinations with String Datatype and int data types:
>>> v1 = 5
>>> v2 = 9
>>> v3 = v1 + v2
>>> print(v3)
14
>>> st1=”string1″
>>> st2=”string2″
>>> st3 = st1 + ” ” + st2
>>> print(st3)
string1 string2Number and Strings mixture is not supported
>>> str1 = “one”
>>> b = 10
>>> c = 5
>>> e = b + c + str1
Traceback (most recent call last):
File “”, line 1, in
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
Libraries
Python libraries like turtle we will test…
Order of operations (good to know for mathematical operators)
System subprocess call using python:-Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(["ls","-l"])
Functions in Python : -
using turtle function
import turtle
def square(turtle):
for i in range(4):
turtle.fd(100)
turtle.lt(90)
techs = turtle.Turtle()
square(techs, 10)
OR
square(techs, length=10)
turtle.mainloop()
built in funtions
>>> type(15)
<class 'int'="">
>>> dir(__builtins__) - this will display the all list of built in functions, you can ignore the errors for now
>>> int(5.6)
5
>>> round(5.6) . see the difference in the output
==========19072019======
Address.remove (remove is a method)
Slicing eg -
s = "Python is fun"
print(s[7:9])
Output will be. - is (so this is index which is removed)
List-
numbers = [1, 2, 3, 4, 5]
numbers.remove(3)
print(numbers)
that's what we get after removing item 3 from the list. Note that we removed item 3, not item with index 3.
numbers = [1, 2, 3, 4, 5]
numbers.remove(3)
print(numbers[3])
In the second line we are removing item 3 from the list so we end up with [1, 2, 4, 5]. From that new list we print out item with index 3 which is item 5.
>>> print(“python is fun”[-3:-1])
fu
That is the correct answer because it extracts items with index -3, and -2 which corresponds with string ‘fu’. Of course -1 is not extracted because slicing is upper bound exclusive.
a = [10, 20, 30]
mysum = a[1] + a[2]
print(mysum)
a = ["10", "20", "30"]
mysum = a[1] + a[2]
print(mysum)
You get 2030 because when adding up strings (i.e. '20' + '30') you get a concatenated string. So, even though items of list a look like integers they are actually strings because they are in quotes.
def foo(x, array):
if x in array:
return True
else:
return False
print(foo(1, [2, 4, 5]))
print(foo(1, [1, 3, 8]))
##code check
def check_username(username):
if len(username) > 4:
return "Successful"
else:
return "Not Correct"
##multiple statements
x = "1"
if isinstance(x, int) or isinstance(x, float) or x =='1':
print("Valid type!")
else:
print("Invalid")
#more on syntxes
a = 5
type(a) == int
#can written like
isinstance(a, int)## will be helpful in later programming sectoins
##Program to use multuple checkes with if statement
import os
os.system("sh systemdetails.sh")
###if char is less then 8 char
charinput = input("Enter any text\n")
def charcheck(charinput):
if len(charinput) < 8:
print(False)
else:
print(True)
##print(charcheck(yyyyyyyy))
###one more ex
def temp(number):
if number >= 8:
return "Hot" ##return is important else u get None error
else:
return "Cold"
print(temp(5))
##more if elif logics
def caltemp(tempvalue):
if tempvalue >= 25 and tempvalue <= 45:
return "Its Hot"
elif tempvalue >= 15 and tempvalue <= 25:
return "Warm"
elif tempvalue >= 46:
return "Its Very Hot"
else:
return "Cold"
print(caltemp(75))
####data type
isinstance("welcome to techsteppers", str)
isinstance([5, 2, 4], list)