Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
46 views

1-First Python Program: Python Programming - (Install Python & Pycharm) Min 00:01

This document provides an introduction to Python programming concepts including: 1. Defining variables to store different data types like integers, floats, strings, and Booleans. 2. Getting input from the user and storing it in a variable. 3. Type conversion between strings and other data types when performing operations. 4. Using strings, including indexing, slicing, and formatted strings to dynamically insert variables.

Uploaded by

mera1990
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

1-First Python Program: Python Programming - (Install Python & Pycharm) Min 00:01

This document provides an introduction to Python programming concepts including: 1. Defining variables to store different data types like integers, floats, strings, and Booleans. 2. Getting input from the user and storing it in a variable. 3. Type conversion between strings and other data types when performing operations. 4. Using strings, including indexing, slicing, and formatted strings to dynamically insert variables.

Uploaded by

mera1990
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python programming –( install Python & pycharm) min 00:01

1- First python program


Print (“name”) program to give your name.

Print (‘O-----‘)
Print (“ ||||’) program to draw a simple dog.
Print (‘*’ * 10) program to multiply character (‘*’) 10 times
Where the (“???“) (‘???‘) Define the item between
Them as a string as a character. And the (*) define
A multiply math operation. This ( ‘*’ * 10) is called a
Code expression
2- Variables
Price=10 price=20 name = ‘Mohammed’ rating=4,9 is_published = True
Print (price) Print (price) Print (name) Print (rating) print (is_new)

Variables written in small litter


In the example above the words rating, name, price, is_published define variables
is _ published , is _new define a Boolean value can be True or False.
price is an integer a number without decimal point. Like 10 or 30
rating is a float a number with a decimal point . Like 5.7 or 30.9
name or cource_name is a string ( a sequence of characters ) . Like ‘Mohammed’ or
‘TV’
ex1 /wright a program

Solution / the executer runs smoothly that mean that the cods that is written have no error
.full_name = ‘Mohammed’
age=30
is_new=True
*Variables name written is small liter python is case sensitive language so like when you wright
Boolean in small liter like this true or false the python doesn’t recognize it so wright form is
True or False .
In this program we store simple values like integers, floats, string, Boolean
(10 , 4.9 , Ahmed , True) .

In python we can store complex values like lists and objects

3- Getting Input (Receiving input ).


Input(‘what is your name’? ) we puts a space after this mark when the user wright a
value the input function return to store the result we put it in a variable called name
name=input(‘what is your name’? )
print (‘hi ‘ + name)
we right a string and combined it with another string that is what we have in the name
variable. its a simple program ask user about his name and return greeting customized
for that user we put a space after (hi) word so a space will be executed and return (hi )to
the name.

ex2/ wright a program

Solution/

name=input(‘what is your name? “)

color=input(‘what is your favorite color? ‘)

print(name+ ‘ likes ’+color)

Executed like this = Mohammed likes Blue


4-Type Conversion
Wright a program that ask about the year you are born in and calculate your age and put it on
to terminal

Input (‘Birth year : ‘)

birth_year= Input ( ‘ Birth year : ‘ )

age = 2020 – birth_year

print(age)

after execute

we see error appear like this the error as it

says its appear in line 2 and show you the

code that generate this error

type error ( unsupported operand type -: ‘int’ and ‘str’ integer is a full number 2019 and
string is a character ’ Birth year’ its like we ar subtracting integer from a string and python
don’t now what to do or haw to evaluate this expertion ( 2019 - ‘1990’) to fix this problem
we nead to convert ‘1990’ into integer 1990 and then we can subtract i- we learned till now
to built in function that is print() and input() we have a few more function for converting
values

we have int() for converting string into an integer float() for converting string into afloat

bool() for converting string to a Boolean value so to fix the upper problem

we wright line 2 like this age = 2020 – int( birth_year ) int()will convert the birth
year to an integer so the python can evaluate the expression

we right before line 2 print(type of (birth_year)) and after line 3 print (type(age))

we see it appear like this <class ‘str’> <class’int’> so we learn wen aver we use the
input() function we goat a string so if you expect a numerical value you need to convert that
string into an integer or a float
ex3/ wright a program

Solution

Weight_lbs =input('what is your weight in pounds (lbs)? ' )

Weight_kg= 0.45 * int(weight_lbs)

print(Weight_kg) min 28:00

5- Strings
Course=’python for beginners’

We use single or double ‘ ’ “ ” to define a string but sometimes you will use a specific
form so if we put like this Course=’python’ s course for beginners’ we se it only define
python as a string and don’t know what to do with the rest so we wright double cot ”” the
true form will be like this .

Course=”python’s course for beginners”

Print (course) (so if you put a double “ “ hear before beginner like this:
Course =”python’ s course for” beginners” we see that the python got confused and
don’t know what this characters (beginners)are so we change the double Quote to single
Quote like this

Course=’python course for “beginners” ‘


Print (course)
If we right a multi-line string like a message sent to email in that case you use a triple cot like
this course = ‘’’ (here we use triple cot to right a multiple
hi john, line string)

Here is our first email to you


Thank you . The support team

‘’’
course=’Python course for beginners’
print(course[0]) Output = P

Here we can use brackets [ ] to gets a character and give an index to this string the index of
the first character in this string in other word this is haw python string are indexed 012345 so
the index of the first character is 0 and so on So if we run the program we get P also we can
use negative index here if we use negative index we see the characters starting from the end
assuming 0 is the index of the first character so -1 is the index of the last character so in -2
we get the second character from the end Print(course[-1]) Output = s
Print(course[-2]) Output = r Pay attention to this brackets [ ] and haw it works
because it’s used in python online and university tests all so we can use this syntax to extract
more character rather than one character for example Print(course[0 : 3]) output = Pyt
will return the character from index 0 to index 3 but exclude the character of index if we use
[0 : ] python will return all the character from the string output Python course for
beginners if we use [ 1 : ] python will exclude the first character like this output = ython
course for beginners if we use it like this [ : 5 ] python will assume the start index is 0
and the output = Pytho if we put it like this [ : ] python will assume the start index is 0 and
the end index is the end of the string . so we can basically copy or clone a string .

If we define another variable here like this after the (course) variable in the example above
variable (another) will be a copy of the variable ( course )

Ex

course=’Python course for beginners’


another = course[:]

print (another) output = Python course for beginners

ex/ define a variable called name and set it to Jennifer and index it like like this 1 : -1
and see the solution

name = ‘jennifer’

print(name[1:-1] ) we will see output = ennife min 37:38


6- Formatted Strings
Useful for dynamically generate some text with your variables if we have two variables
for example first name and last name so if we want to generate some text like this
John [smith] is a coder lets say we want to print this on to terminal haw we do this
we define another variable like message= first + ‘ [ ’+last+’ ] is a coder ’ first
name plus astringe with a space and open bracket plus last name and plus a string
with the closed bracket and the text we want this approach is not ideal because as our
text become more complicated its become harder to vowelize our output so if some
one else reading this code they had to visualize all this cachinnation first + ‘
[ ’+last+’ ] is a coder ’ in there head that’s way we use a formatted strings it make it
easier to visualize the output so we define another variable with a formatted string
that is pre fixed with an f msg = f‘ {first} [ { last} ] is a coder ‘ and a quote with
the value of the first name between { } and space and then we put our bracket [ ] in
between this bracket we put { } and between them our last name f‘ {first}
[ { last} ] is a coder ‘ this is what we called a formatted string in this { } we define a
place holders or holes in our string and wen we run the program this holes will be filled
with the value of our variables so to define a formatted string pre fixed your string with
an f ‘ ‘ and use { } to dynamically insert values into your string

first = ‘john’
last =’smith’
message= first + ‘ [ ’+last+’ ] is a coder ’
msg = f‘ {first} [ { last} ] is a coder ‘
print(message) output = John [smith] is a coder
print(msg) output = John [smith] is a coder

min 40:50

You might also like