Python Basic
W3School
64 flashcards · Programming Languages · by Daniel Blackbird
print hello world
print("hello,world")
who make python and when ?
Guido van Rossum, and released in 1991
how space syntax work?
if 5 > 2: print("Five is greater than two!") -------- error if 5 > 2: print("Five is greater than two!")
one line comment in python
#This is a comment
multi line comment in python
""" This is a comment written in more than just one line """
int x =5 in python
x=5 :))))
int x=5 ; string x="name" in python
x=5 x="name"
different between x="name" x='name' in python?
no difference
int x=2; float y=2.2; complex z=2j; in python ?
x=2; y=2.2; z=2j;
how to know type of a variable by code in python?
type(variable)
y=12*10^4 in python and what type is this .
y = 12E4 float
int x = 1; how to convert it to float by code ?
x = 1 # int y = 2.8 # float z = 1j # complex #convert from int to float: a = float(x) #convert from float to int: b = int(y) #convert from int to complex: c = complex(x)
Print random number between 1 and 10 in python .
import random print(random.randrange(1,10))
3 way casting a data to int float or string
x = int(1) # x will be 1 y = int(2.8) # y will be 2 z = int("3") # z will be 3 x = float(1) # x will be 1.0 y = float(2.8) # y will be 2.8 z = float("3") # z will be 3.0 w = float("4.2") # w will be 4.2 x = str("s1") # x will be 's1' y = str(2) # y will be '2' z = str(3.0) # z will be '3.0'
assign a whole text to a varriable by code .
#three = > " " " a = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."""
write a code print second word of "Hello World!" example : e
a = "Hello, World!" print(a[1])
write a code print Hello from "Hello World" phrase
b = "Hello, World!" print(b[0:5])
write a code that delete space from first and end of a string " Hello , World! "
a = " Hello, World! " print(a.strip()) # returns "Hello, World!"
write a code that show length of a string
a = "Hello, World!" print(len(a))
write a code that convert to lowercase string (Hello,World)
a = "Hello, World!" print(a.lower())
write a code that convert to uppercase string (Hello,World)
a = "Hello, World!" print(a.upper())
write a code that Replace a word of string to other word . example:Hello World! => (H to J ) => Jello World
a = "Hello, World!" print(a.replace("H", "J"))
write a code that split a strings to substrings example : Hello World! => returns ['Hello', ' World!']
a = "Hello, World!" print(a.split(","))
write a code that combine two variables(string,integer) example : age = 21 , i am [age] years === i am 21 years old
age = 36 txt = "My name is John, and I am {}" print(txt.format(age))
write a code that combine string and integer but chose order by string
quantity = 3 itemno = 567 price = 49.95 myorder = "I want to pay {2} dollars for {0} pieces of item {1}." print(myorder.format(quantity, itemno, price))
Python Arithmetic Operators
+ - * / % // **
Python Assignment Operators
= += -+ *= /= %= //= **= &= |= ^= >>= <<=
Python Comparison Operators
== != > < >= <=
Python Logical Operators
and or not
Python Identity Operators
is is not
Python Membership Operators
in not in
Python Bitwise Operators
& | ^ ~ << >>
write a code that make a list contains [apple, banana, cherry]
thislist = ["apple", "banana", "cherry"] print(thislist)
create a list and print first element
thislist = ["apple", "banana", "cherry"] print(thislist[1])
create a list and change first element and and print first element
thislist = ["apple", "banana", "cherry"] thislist[1]="moooz" print(thislist[1])
write a code that make a list and print whole list by for loop
thislist = ["apple", "banana", "cherry" , "maskhare"] for x in thislist: print(x)
write a code that if item1 exist in list print yes.
thislist = ["apple", "banana", "cherry"] if "apple" in thislist: print("Yes, 'apple' is in the fruits list")
write a code that make a list and print length of list
thislist = ["apple", "banana", "cherry"] print(len(thislist))
write a code that make a list and add a item to it
thislist = ["apple", "banana", "cherry"] thislist.append("orange") print(thislist)
write a code that make a list then add a item to a index of list
thislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange") print(thislist)
write a code that make a list and remove a string from it example : apple orange banana remove orange from it
thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)
write a code that make a list and pop a first item and last item from it .(without know index of last item) example :list = apple , banana ,cherry print=> banana
thislist = ["apple", "banana", "cherry"] thislist.pop() thislist.pop(0) print(thislist)
write a code that make a list and remove a item by index.
thislist = ["apple", "banana", "cherry"] del thislist[0] print(thislist)
write a code that make a list and remove list completely
thislist = ["apple", "banana", "cherry"] del thislist
write a code that make a list and clear list items
thislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist)
can i use list2=list1 as copy list1 to list2?why?
no,because when we use this code ,list2 will reference to list1 and when we change list2 ,it will changes list1 because it has same address
write a code that make a list and copy that list to another list. two method
thislist = ["apple", "banana", "cherry"] mylist = thislist.copy() print(mylist) -------- thislist = ["apple", "banana", "cherry"] mylist = list(thislist) print(mylist)
write a code that make a list with list() constructor
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets print(thislist)
write a code that make a tuple and print it
thistuple = ("apple", "banana", "cherry") print(thistuple)
what is tuple in python?
A tuple is a collection which is ordered and unchangeable.
write a code that print second items in tuple.
thistuple = ("apple", "banana", "cherry") print(thistuple[1])
write a code that make a tuple and print it by for loop
thistuple = ("apple", "banana", "cherry") for x in thistuple: print(x)
write a code that make a tuple and check condition if answer is true print "yes"
thistuple = ("apple", "banana", "cherry") if "apple" in thistuple: print("Yes, 'apple' is in the fruits tuple")
write a code that make a tuple and print length of tuple
thistuple = ("apple", "banana", "cherry") print(len(thistuple))
is it possible add item to tuple?change tuple item?remove item from tuple ?
no,tuple is unchangeable
is it possible that remove tuple completely ?if yes how?
thistuple = ("apple", "banana", "cherry") del thistuple print(thistuple) #this will raise an error because the tuple no longer exists
write a code that make a tuple by constructor
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets print(thistuple)
what is set in python?
A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.
write a code that make a set and print it
thisset = {"apple", "banana", "cherry"} print(thisset)
write a code that make a set and print it by loop
thisset = {"apple", "banana", "cherry"} For x in thisset print x ------- result may change every times .
write a code that make a set and and print True Or False if a item available in set or not .
thisset = {"banana","apple","orange"} print("bananna" in thisset)
write a code that make a set and update it by addition info exp:set= apple ,orange,bannana update= apple,orange ,banana ,cherry
thisset("apple","orange","banana") thisset.update(["apple","orange","banana","cherry"]) print(thisset)
write a code that make a set and print length of set
thisset("apple","orange","banana") print(len(thisset))
write a code and make a set and then remove it by two way . what different between to ways?
thisset("apple","orange","banana") thisset.discard() thisset.remove() ------------------------------------------------- remove => if set not available then error occur but discard no error