Computer Programming: A Step-by-Step Guide to Learn Python, SQL, C++, C#, Raspberry Pi, and Data Science
By Vere salazar
()
About this ebook
Computer Programming' is a comprehensive and practical guide that teaches you how to program in various languages and technologies, from beginner to advanced levels. You will learn:
How to use Python, the most popular and versatile programming language, for various applications and domains, such as web development, data analysis, machine learning, and more.
How to manipulate and analyze data with SQL, the standard language for relational databases, and perform common operations such as creating, querying, updating, and deleting data.
How to create games and apps with C++ or C#, two powerful and widely used languages for object-oriented and low-level programming, and understand the fundamentals of game design and development.
How to explore the possibilities of Raspberry Pi, the small and affordable computer that can run various operating systems and languages, such as Scratch and Python, and use it for fun and educational projects.
How to apply your programming skills to real-world scenarios, with plenty of examples and exercises that will help you practice and improve your coding abilities.
'Computer Programming' is more than just a book; it's a journey that will take you from novice to expert in the exciting and rewarding field of computer programming.
Read more from Vere Salazar
Python Machine Learning: Using Scikit Learn, TensorFlow, PyTorch, and Keras, an Introductory Journey into Machine Learning, Deep Learning, Data Analysis, Algorithms, and Data Science Rating: 0 out of 5 stars0 ratingsMastering Python and Raspberry Pi: A Comprehensive Guide to Computer Programming Rating: 0 out of 5 stars0 ratingsC++ for Beginners: A Complete Course to Master the Fundamentals of C++ Programming Rating: 0 out of 5 stars0 ratingsC# for beginners: A step-by-step guide to developing professional and modern applications Rating: 0 out of 5 stars0 ratingsHacking with Kali Linux: A Beginner’s Guide to Cybersecurity and Penetration Testing Rating: 0 out of 5 stars0 ratingsLinux Hacking 101: A Beginner’s Guide to Ethical Hacking with Linux and Kali Linux Rating: 0 out of 5 stars0 ratingsLinux Basics for Beginners: A Step-by-Step Guide for Hackers and Cybersecurity Enthusiasts Rating: 0 out of 5 stars0 ratingsPython programming: A Quick and Easy Approach to Learning Python Programming Rating: 0 out of 5 stars0 ratingsMastering SQL: A Step-by-Step Guide toSQL Programming and Database Management Systems for Beginners Rating: 0 out of 5 stars0 ratingsPython Data Science: A Comprehensive Guide to Self-Directed Python Programming Learning Rating: 0 out of 5 stars0 ratingsRaspberry Pi: The Ultimate Guide to Learning the Fundamentals and Unleashing Powerful Hacks Rating: 0 out of 5 stars0 ratingsSQL for Beginners: A Guide to Excelling in Coding and Database Management Rating: 0 out of 5 stars0 ratingsEthical Hacking: How to Protect Your Network from Cyberattacks Rating: 0 out of 5 stars0 ratingsCoding for Beginners: A Step-by-Step Guide to Learn Python, Java, SQL, C, C++, C#, HTML, and CSS from Scratch Rating: 0 out of 5 stars0 ratingsRaspberry PI: The Comprehensive Guide to Self-Taught Computer Technology Learning, Simple Setup, and Project Mastery Rating: 0 out of 5 stars0 ratingsKali Linux for Beginners: A Step-by-Step Guide to Learn the Basics of Hacking and Security Testing Rating: 0 out of 5 stars0 ratingsPython for Beginners: This comprehensive introduction to the world of coding introduces you to the Python programming language Rating: 0 out of 5 stars0 ratingsMastering Python: A Comprehensive Guide for Beginners to Learn the Basics Rating: 0 out of 5 stars0 ratings
Related to Computer Programming
Related ebooks
SQL for Beginners: A Guide to Excelling in Coding and Database Management Rating: 0 out of 5 stars0 ratingsSQL in 30 Pages Rating: 4 out of 5 stars4/5SQL Mastery: A Step-by-Step Guide to Learn SQL and Manage Data Effectively Rating: 0 out of 5 stars0 ratingsComputer Programming: A Simplified Entry to Python, Java, and C++ Programming for Beginners Rating: 0 out of 5 stars0 ratingsPython for Beginners: A Step-by-Step Guide to Master the Basics of Python with Easy Projects Rating: 0 out of 5 stars0 ratingsSQL Mastery: The Masterclass Guide to Become an SQL ExpertMaster The SQL Programming Language In This Ultimate Guide Today! Rating: 0 out of 5 stars0 ratingsA Beginner's guide to Python Rating: 0 out of 5 stars0 ratingsSQL Made Simple Rating: 0 out of 5 stars0 ratingsPython Programming For Beginners: Python Programming Language Tutorial Rating: 0 out of 5 stars0 ratings#1 Book on Python Programming Rating: 0 out of 5 stars0 ratingsPython and SQL Bible: From Beginner to World Expert: Unleash the true potential of data analysis and manipulation. Rating: 0 out of 5 stars0 ratingsRaspberry Pi Computer Architecture Essentials Rating: 0 out of 5 stars0 ratingsSQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5SQL Mastery: From Novice Queries to Advanced Database Wizardry Rating: 0 out of 5 stars0 ratingsEasy Programming for Everyone Rating: 0 out of 5 stars0 ratingsBeginning T-SQL with Microsoft SQL Server 2005 and 2008 Rating: 3 out of 5 stars3/5Quick Python Guide Rating: 0 out of 5 stars0 ratingsSQL Demystified: A Beginner's Roadmap to Data Retrieval and Management Rating: 0 out of 5 stars0 ratingsIntroduction to Python Programming: Do your first steps into programming with python Rating: 0 out of 5 stars0 ratingsPython for Absolute Beginners: Learn to Code Fast! Rating: 0 out of 5 stars0 ratingsLearn SQL using MySQL in One Day and Learn It Well: SQL for beginners with Hands-on Project Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 4 out of 5 stars4/5Mastering Python: A Comprehensive Crash Course for Beginners Rating: 0 out of 5 stars0 ratingsPython Simplified: Learn Programming Through Practical Examples Rating: 0 out of 5 stars0 ratingsPython Data Science: A Comprehensive Guide to Self-Directed Python Programming Learning Rating: 0 out of 5 stars0 ratingsBeginner's guide to mastering python Rating: 0 out of 5 stars0 ratingsBeginners' Artificial Intelligence and Python Programming Rating: 0 out of 5 stars0 ratings
Programming For You
Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsBeginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsPython for Data Science For Dummies Rating: 0 out of 5 stars0 ratingsC Programming Language, A Step By Step Beginner's Guide To Learn C Programming In 7 Days. Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5JavaScript Bootcamp: From Zero To Hero: Hands-On Learning For Web Developers Rating: 0 out of 5 stars0 ratingsWindows 11 For Dummies Rating: 0 out of 5 stars0 ratingsTeach Yourself C++ Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5
Reviews for Computer Programming
0 ratings0 reviews
Book preview
Computer Programming - Vere salazar
Computer programming
A Step-by-Step Guide to Learn Python, SQL, C++, C#, Raspberry Pi, and Data Science
Vere Salazar
Copyright © [2024] all rights reserved.
No portion of this book may be reproduced in any form without written permission from the publisher or author, except as permitted by u.s. Copyright law.
This publication is designed to provide accurate and authoritative information in regard to the subject matter covered. It is sold with the understanding that neither the author nor the publisher is engaged in rendering legal, investment, accounting or other professional services. While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional when appropriate. Neither the publisher nor the author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, personal, or other damages.
Table of contents
Book 1: python for beginners
Introduction
Chapter 1: datatype in python
Chapter 2: variables operator the types and their use
Chapter 3: loops and function - variable function in python
Chapter 5: object-oriented programming
Chapter 6: python list basics
Chapter 7: python work list - tuples in python
Chapter 8: classes
Chapter 9: flow control
Chapter 10: python dictionary
Chapter 11: make your codes
Conclusion
Book 2: python data science
Introduction
Chapter 1: what is the difference between data science and analysis
Chapter 2: numpy introduction
Chapter 3: manipulation data with pandas
Chapter 4: visualization with matplotlib
Chapter 5: machine learning
Conclusion
Book 3: sql for beginners
Introduction
Chapter 1: what is sql
Chapter 2: control flow tools
Chapter 3: tool and database structure
Chapter 4: tables
Chapter 5: learning function
Chapter 9: data types
Chapter 10: 9 common mistakes and how to avoid them
Conclusion
Book 4:c++ for beginners
Introduction
Chapter 1: anatomy of c++
Chapter 2: declaring constants
Chapter 5: operator type and overloading
Chapter 6: macros and template
Chapter 7: classes
Chapter 8: library
Chapter 9: stl
Conclusion
Book 5: c# for beginners
Introduction
Chapter 1: anatomy of c#
Chapter 2: data type
Chapter 3: operators variable
Chapter 4: string and list
Chapter 5: syntax
Chapter 6: classes
Chapter 7: linq, queries, operators – xaml
Chapter 8: program to make decision
Chapter 9: net
Chapter 10: enum and struct
Conclusion
Book 6: raspberry pi
Introduction
Chapter 1: basics of raspberry pi
Chapter 2: setup
Chapter 4: interfacing hardware
Chapter 5: download the operating system
Chapter 6: projects
Chapter 7: raspberry with python and linux
Chapter 8: raspberry pi models
Conclusion
Book 1: python for beginners
The definitive beginner's guide to the realm of coding, introducing you to the programming language python
Introduction
The, python coding language, is a gre,at way to start your journe,y by le,arning more, about compute,rs, le,arning how to code,, and e,ve,n making some, of your game,s and apps. this is one, of the, be,st language,s to ge,t starte,d be,cause, it is simple,, base,d on the, e,nglish language,, and many of those, who de,cide, to ge,t starte,d with a coding language, will naturally fall with this one,. this guide,book is going to take, some, time, to discuss the, python language, and he,lp you to le,arn some, of the, basics that come, with it to he,lp you to ge,t starte,d.
While, the, python language, is a simple, and basic language, that is pe,rfe,ct for the, be,ginne,r, it also has a lot of powe,r that you are, going to e,njoy whe,n cre,ating your code,s. inside, this guide,book, you are, going to le,arn e,ve,rything that you ne,e,d to ge,t starte,d with the, python language, and to make, it work we,ll for your ne,e,ds!
This book is primarily for pe,ople, who are, re,lative,ly ne,w to programming and, more, spe,cifically, those, who want to discove,r the, world of python. this book will take, you through the, fundame,ntals of programming and python.
Compute,r programming sounds scary, but it re,ally isn’t. the, harde,st part is choosing which language, you want to le,arn be,cause, the,re, are, so many to choose, from. python is one, of the, e,asie,st of all of compute,r programming language,s; inde,e,d, pre,tty much e,ve,rything you ne,e,d is right the,re,, at your disposal. all you ne,e,d to do is le,arn how to use, what the, program give,s you to write, programs.
Go ahe,ad and re,ad through the, e,ntire, book, ge,t the, knowle,dge,, and be, informe,d about the, ke,y things that you ne,e,d to know about this particular topic.
If you are, as e,xcite,d as i am to le,arn e,ve,rything python and how to program with it, le,t us ge,t starte,d.
Chapte,r 1: datatype, in python
The, python data type,s
The, ne,xt thing that we, ne,e,d to take, a look at is the, python data type,s. e,ach value, in python has a type, of data.
Python numbe,rs
The, first option that we, can work on python data include,s the, python numbe,rs. the,se, are, going to include, things like, comple,x numbe,rs, floating-point numbe,rs, and e,ve,n inte,ge,rs. the,y are, going to be, de,fine,d as comple,x, float, and int classe,s in python. for e,xample,, we, can work with the, type,() function to ide,ntify which cate,gory a value, or a variable, affiliate,d with to, and the,n the, isinstance,() function to audit if an obje,ct e,xists to a distinct class.
Whe,n we, work with inte,ge,rs can be, of any le,ngth, it is going only to find limitations in how much me,mory you have, available, on your compute,r. the,n the,re, is the, floating-point numbe,r.
This is going to be, accurate, up to 15 de,cimal place,s, though you can go with a smalle,r amount as we,ll.
The, floating points are, going to be, se,parate,d by a de,cimal point. 1 is going to be, an inte,ge,r, and 10 will be, a floating-point numbe,r.
And finally, we, have, comple,x numbe,rs. the,se, are, going to be, the, numbe,rs that we, will want to write, out as x + y, whe,re, x is going to be, the, re,al point, and the,n the,y are, going to be, the, imaginary part.
We, ne,e,d to have, the,se, two put toge,the,r to make, up the, comple,xity that we, ne,e,d with this kind of numbe,r.
Python lists
The, python list is going to be, a re,gulate,d se,rie,s of ite,ms. it is going to be, one, of the, data type,s that are, use,d the, most in python, and it is e,xce,e,dingly re,sponsive,.
All of the, ite,ms that will show up on the, list can be, similar, but this is not a re,quire,me,nt. you can work with a lot of diffe,re,nt ite,ms on your list, without the,m be,ing the, same, type,, to make, it e,asie,r to work with.
Be,ing able, to de,clare, a list is going to be, a straightforward option that we, can work with. the, ite,ms are, going to be, se,parate,d by commas, and the,n we, just ne,e,d to include, the,m inside, some, bracke,ts like, this: [ ] we, can also e,mploy the, slicing ope,rator to he,lp us obtain out a pie,ce, or a se,le,ction of ite,ms out of that list.
The, inde,x starts at 0 in python.
And we, have, to re,me,mbe,r while, working on the,se, that lists are, going to be, mutable,.
It me,ans that the, value, of the, e,le,me,nts that are, on your list can be, alte,re,d or update,d.
The, lists in python are, going to be, the, most basic data structure, be,cause, the,y are, going to fall in a se,que,nce,.
The, e,le,me,nts that are, inside, of the, list are, all going to have, a numbe,r that is assigne,d to the,m. in othe,r words, the,y all have, a place, that is assigne,d to the,m.
Not only are, the,y simple,, but lists are, going to be, ve,rsatile,.
Whe,n cre,ating a list, the, obje,cts are, going to be, se,parate,d by a comma, and all of the,se, obje,cts are, going to fall inside, a se,t of square, bracke,ts.
Ite,ms in a list are, not going to have, to be, of the, same, data type,.
E,xample,
First list = [ ‘ scie,nce,’, ‘history,’ , 2002, 2030 ] ;
Se,cond list = [ 2, 4, 6 ,8 ,10 ];
Third list = [ b
, d
, e,
, g
]
The, inde,x for a list is going to start at ze,ro, and you are, going to have, the, ability to cut it so that you can cre,ate, sub lists ge,tting value,s inside, of your list
Whe,n you want to ge,t a value, inside, of the, list so that you can slice, it at a spe,cific inde,x.
E,xample,
#! usr bin/ python
First list = [ ‘ scie,nce,’, ‘history,’ , 2002, 2030 ] ;
Se,cond list = [ 2, 4, 6 ,8 ,10 ];
Print first list [ 2 ] :
, fist list [ 2 ]
Print se,cond list [ 2:4 ] :
, se,cond list [ 2 : 4 ]
Output
First list [ 2 ]: history
Se,cond list [ 2:4 ] : [ 4, 6, ]
Python strings
Python strings are, a se,rie,s of characte,rs e,nclose,d within quote,s. use, any type, of quote,s to e,nclose, python strings, that is, e,ithe,r single,, double,, or triple, quote,s. to acce,ss string e,le,me,nts, we, use, the, slice, ope,rator. string characte,rs be,gin at inde,x 0, me,aning that the, first characte,r string is at inde,x 0. this is good whe,n you ne,e,d to acce,ss string characte,rs. to concate,nate, strings in python, we, use, + ope,rator, the, aste,risk 9*) is use,d for re,pe,tition. e,xample,:
#!Usrbin/python3
Thanks = ‘thank you’
Print (thanks) # to print the, comple,te, string
Print (thanks[0]) # to print the, first characte,r of string
Print (thanks[2:7]) # to print the, 3rd to the, 7th characte,r of string
Print (thanks[4:]) # to print from the, 5th characte,r of string
Print (thanks * 2) # to print the, string two time,s
Print (thanks + \tagain!
) # to print a concate,nate,d string
The, program prints the, following once, e,xe,cute,d:
Image00003.jpgNotice, that we, have, te,xt be,ginning with # symbol. the, symbol de,note,s the, be,ginning of a comme,nt. the, python print will not act on the, te,xt from the, symbol to the, e,nd of the, line,. comme,nts are, me,ant to e,nhance, the, re,adability of code, by e,xplaining. we, de,fine,d a string name,d thanks with the, value, thank you. the, print (thanks[0]) state,me,nt he,lps us acce,ss the, first characte,r of the, string. he,nce, it prints t. you also notice, that the, space, be,twe,e,n the, two words is counte,d as a characte,r.
We, can work with e,ithe,r a single, quote, or a double, quote, to show off our strings, but we, ne,e,d to make, sure, that the, type, of quote, that we, use, at the, be,ginning is the, one, that we, finish it off with, or we, will cause, some, confusion with the, compile,r.
We, can e,ve,n work with multi-line, strings with the, he,lp of a triple, quite,.
Like, what we, are, going to se,e, whe,n we, use, the, tuple, or the, list that we, talke,d about above,, the, slicing ope,rator is some,thing that we, can use, with our string as we,ll. and just like, with what we, se,e, in the, tuple,s, we, will find that the, string is going to be, immutable,.
Python se,t
Ne,xt on the, list is going to be, the, python se,t. the, se,t is going to be, an option from python that will include, an unorde,re,d colle,ction of unique, ite,ms. the, se,t is going to be, de,fine,d by value,s that we, can se,parate, with a comma in brace,s. the, e,le,me,nts in the, batch are, not going to be, orde,re,d, so we, can use, the,m in any manne,r that we, would like,.
We, have, the, option to pe,rform this se,t of ope,rations at the, same, time, as a union or have, an inte,rse,ction on two se,ts.
The, se,ts that we, work with are, going to be, unique, value,s, and the,y will make, sure, that we, e,liminate, the, duplicate,s since, the, se,t is going to be, an unorde,re,d compilation. cataloge,d has no aim.
The,re,fore, the, slicing ope,rator is not going to work for this kind of option.
Python lists
Lists consist of ite,ms e,nclose,d within square, bracke,ts ([]), and the, ite,ms are, se,parate,d using commas (,). the,y are, similar to the, c arrays. although all array e,le,me,nts must be,long to a similar type,, lists support the, storage, of ite,ms be,longing to diffe,re,nt type,s in a single, list.
We, use, the, slice, ope,rator ([ ] and [:]) for acce,ssing the, e,le,me,nts of a list. the, indice,s start at 0 and e,nd at -1. also, the, plus symbol (+) re,pre,se,nts the, concate,nation ope,rator, while, the, aste,risk (*) re,pre,se,nts the, re,pe,tition ope,rator. e,xample,:
#!Usrbin/python3
Lista = [ ‘john’, 3356 , 8.90, ‘siste,r’, 34.21 ]
Listb = [120, ‘siste,r’]
Print lista # will print the, comple,te, list
Print lista[0] # will print the, first e,le,me,nt of the, list
Print lista[1:3] # will print the, e,le,me,nts starting from the, 2nd till 3rd
Print lista[2:] # will print the, e,le,me,nts starting from the, 3rd e,le,me,nt
Print listb * 2 # will print the, list two time,s
Print lista + listb # will print a concate,nate,d lists
Image00004.jpg The,re, is no much diffe,re,nce, in what is happe,ning in the, above, code, compare,d to the, pre,vious one, for strings. whe,n e,xe,cute,d, the, program outputs:
In the, state,me,nt print lista
, we, print the, conte,nts of lista. note, that e,ach e,le,me,nt is tre,ate,d to be, at its inde,x as a whole,, for e,xample,, e,le,me,nt ‘john’ is tre,ate,d as a single, e,le,me,nt of a list at inde,x 0.
Python tuple,s
Tuple,s are, going to be, like, lists in the, fact that the,y are, going to be, a se,que,nce, of obje,cts, but the,y are, going to be, immutable,.
You can use, a se,t of pare,nthe,se,s or not make, a tuple,, all you ne,e,d to e,nsure, is that you are, using commas be,twe,e,n the, diffe,re,nt obje,cts that you want in your tuple,.
E,xample,
Tup1 = ( ‘scie,nce,’ , ‘history’, 2002, 2030 )
Tup2 = ( 5, 9, 7, 4, 2 )
Tup3 = f
, b
, e,
, t
To write, a tuple, that is going to be, re,turne,d as e,mpty, it is going to use, the, tuple, function and a se,t of e,mpty pare,nthe,se,s.
We, can also work with some,thing that is known as a python tuple,. the, tuple, is going to be, an orde,re,d se,rie,s of compone,nts that is the, duplicate, as a list, and it is some,time,s hard to se,e, how the,se, are, going to be, similar and how the,y are, going to be, diffe,re,nt.
The, gigantic dive,rsity that we, are, going to se,e, with a tuple, and a list is that the, tuple,s are, going to be, immutable,.
Tuple,s, once, you cre,ate, the,m, are, not modifiable,.
Tuple,s are, applie,d to write,-prote,ct data, and we, are, ge,ne,rally quicke,r than a list, as the,y cannot shift active,ly. it is going to be, de,te,rmine,d with pare,nthe,se,s (), whe,re, the, ite,ms are, also going to be, se,parate,d by a comma as we, se,e, with the, lists.
We, can the,n use, the, slicing ope,rator to he,lp us wring some, of the, compone,nts that we, want to use,, but we, still are, not able, to change, the, value, while, we, are, working with the, code, or the, program.
Python tuple,s are, similar to lists with the, diffe,re,nce, be,ing afte,r cre,ating a tuple,; you cannot add, de,le,te,, or change, the, tuple, e,le,me,nts. tuple, e,le,me,nts should be, e,nclose,d within pare,nthe,sis (). e,xample,:
#!Usrbin/python3
T1 = () # cre,ating an e,mpty tuple,, that is, no data
T2 = (22,34,55)
T3 = tuple,([10,23,78,110,89]) # cre,ating a tuple, from an array
T4 = tuple,(xyz
) # cre,ating tuple, from a string
Print t1
Print t2
Print t3
Image00005.jpg Print t4
The, value,s of the, 4 tuple,s will be, printe,d:
The,re, are, a numbe,r of functions that can be, applie,d on tuple,s.
E,xample,:
#!Usrbin/python3
T1 = (23, 11, 35, 19, 98)
Print(the, minimum e,le,me,nt in the, tuple, is
, min(t1))
Print(the, sum of tuple, e,le,me,nts is
, sum(t1))
Print(the, maximum e,le,me,nt in the, tuple, is
, max(t1))
Print(the, tuple, has a le,ngth of
, le,n(t1))
Whe,n e,xe,cute,d, it give,s this re,sult:
Image00006.jpgFirst, we, calle,d the, min() function, which re,turns the, smalle,st e,le,me,nt in the, tuple,. we, the,n calle,d the, sum() function, which re,turne,d the, total sum of tuple, e,le,me,nts. the, max() function re,turne,d the, maximum e,le,me,nt in the, tuple,. the, le,n() function counte,d all e,le,me,nts in the, tuple, and re,turne,d the,ir numbe,r.
You can use, the, slice, ope,rator to acce,ss some, of the, tuple, e,le,me,nts, not all. e,xample,:
#!Usrbin/python3
T = (23, 26, 46, 59, 64)
Image00007.jpg Print(t[0:2])
Whe,n e,xe,cute,d, it prints:
We, have, use,d the, slice, ope,rator to acce,ss e,le,me,nts from inde,x 0 to inde,x 2 in the, tuple,. note, that tuple, e,le,me,nts be,gin at inde,x 0.
Python dictionarie,s
Python dictionarie,s are, use,d for the, storage, of ke,y-value, pairs. with dictionarie,s, you can use, a ke,y to re,trie,ve,, re,move,, add, or modify value,s. dictionarie,s are, also mutable,, me,aning you can’t the,ir value,s once, de,clare,d.
To cre,ate, dictionarie,s, we, use, curly brace,s. e,ve,ry dictionary ite,m has a ke,y, the,n followe,d by a colon, the,n a value,. the, ite,ms are, se,parate,d using a comma (,). e,xample,:
#!Usrbin/python3
Classmate,s = {
‘john’ : ‘234-221-323‘,
‘alice,’ : ‘364-32-141’
}
We, have, cre,ate,d a dictionary name,d classmate,s with two ite,ms. note, that the, ke,y must be, of a hashable, type,, but you may use, any value,. e,ach dictionary ke,y must be, unique,. i first e,le,me,nt, john, is the, ke,y, followe,d by the, value,. in the, se,cond e,le,me,nt, alice, is the, e,le,me,nt. to acce,ss dictionary e,le,me,nts, use, the, dictionary name, and the, ke,y. e,xample,:
#!Usrbin/python3
Classmate,s = {
‘john’ : ‘234-221-323‘,
‘alice,’ : ‘364-32-141’
}
Print(the, numbe,r for john is
, classmate,s[‘john’])
Image00008.jpg Print(the, numbe,r for alice, is
, classmate,s[‘alice,’])
The, last two state,me,nts he,lp us acce,ss the, dictionary value,s. it prints:
To know the, dictionary le,ngth, run the, le,n() function as follows:
Le,n(classmate,s)
The, above, will re,turn 2 as the, dictionary has only two e,le,me,nts.
Whe,n working with python, a dictionary is going to be, de,cide,d inside, brace,s, with e,ve,ry compone,nt be,ing a combination in the, form of ke,y: value,. the, ke,y and the, value, can be, any type, that you would like, base,d on the, kind of code, that you would like, to write,. we, can also use, the, ke,y to he,lp us re,trie,ve, the, re,spe,ctive, value, that we, ne,e,d. but we, are, not able, to turn this around and work it in that manne,r at all.
Working with the, diffe,re,nt type,s of data is going to be, so important for all of the, work that you can do in a python coding, and can he,lp you out whe,n it is time, to work with data scie,nce,.
Take, a look at the, diffe,re,nt type,s of data that are, available, with the, python language,, and se,e, how gre,at this can be, to any of the, code,s and algorithms that you want to write, into your data scie,nce, proje,ct ove,rall.
Datatype, conve,rsion
Python allows you to conve,rt data from one, type, to anothe,r. the, proce,ss of conve,rting from one, data type, to anothe,r is known as type,casting.
If you ne,e,d to conve,rt your int datatype, into a float, you call the, float() function.
E,xample,:
#!Usrbin/python3
He,ight=20
Print(the, value, of he,ight in int is
, he,ight)
Print(the, value, of he,ight in float is
, float(he,ight))
In the, above, e,xample,, he,ight has be,e,n initialize,d to 20. we, have, calle,d the, float() function and passe,d he,ight to it as the, parame,te,r. the, inte,ge,r value,, that is, 20 has be,e,n conve,rte,d into a float value,, that is, 20.0. the, program prints the, following:
Image00009.jpgTo conve,rt a float into an int, you call the, int() function. e,xample,:
#!Usrbin/python3
He,ight=20.0
Print(the, value, of he,ight in float is
, he,ight)
Image00010.jpg Print(the, value, of he,ight in int is
, int(he,ight))
The, program prints the, following:
We, have, calle,d the, int() function and passe,d the, parame,te,r he,ight to it. it has conve,rte,d 20.0 to 20, which is a float to inte,ge,r conve,rsion.
If you ne,e,d to conve,rt a numbe,r to a string, you call the, str() function. the, numbe,r will the,n be, conve,rte,d into a string.
E,xample,:
#!Usrbin/python3
Num=20
Print(the, value, of num in int is
, num)
Image00011.jpg Print(the, value, of num in string is
, str(num))
The, program outputs:
Although the, value, is the, same,, it is tre,ate,d diffe,re,ntly by a python inte,rpre,te,r. the, conve,rsion of a float to a string can also be, done, similarly.
Chapte,r 2: variable,s ope,rator the, type,s and the,ir use,
Python ope,rators
Ope,rators are, symbols that indicate, the, imple,me,ntation of a spe,cific proce,ss. the,y are, use,d to manipulate, data within a program. python use,s diffe,re,nt type,s of ope,rators.
Arithme,tic ope,rators
Addition (+)
The, addition ope,rator adds the, ope,rands on both side,s of the, ope,rator:
>>>12 + 8
20
Subtraction (-)
The, subtraction ope,rator subtracts the, value, of the, right ope,rand from that of the, le,ft ope,rand.
>>>40 – 3
37
Multiplication (*)
The, multiplication ope,rator multiple,s the, value,s on the, le,ft and right side, of the, ope,rator:
>>>20 * 3
60
Division ( /)
The, division ope,rator divide,s the, le,ft ope,rand with the, value, of the, right ope,rand:
>>>20 / 5
4.0
E,xpone,nt (**)
The, e,xpone,nt ope,rator raise,s the, first ope,rand to the, powe,r indicate,d by the, se,cond ope,rand.
>>> 4**4
256
Modulos (%)
The, modulos ope,rator re,turns the, re,mainde,r afte,r pe,rforming a division of the, le,ft ope,rand by the, right ope,rand.
>>> 38%5
3
Floor division (//)
The, floor division ope,rator pe,rforms a division of the, le,ft ope,rand by the, right ope,rand and re,turns the, quotie,nt as a whole, numbe,r while, dropping the, de,cimal numbe,rs.
>>> 35//3
Image00012.jpg 11
Comparison ope,rators
Logical ope,rators
Python supports 3 logical ope,rators:
Or
And
Not
X or y if x is false,, the,n it e,valuate,s y. e,lse,, it re,turns the, e,valuation of x.
X and y if x is false,, the,n it re,turns the, e,valuation of x. e,lse,, if x is true,, it e,valuate,s y.
Not x if x is false,, it re,turns true,. e,lse,, if x is true,, it re,turns false,.
6
Input and output
Input
Many programs re,quire, the, use,r’s input to function. input can come, from source,s such as ke,yboard, mouse, clicks, database,, e,xte,rnal storage,, or the, inte,rne,t. the, ke,yboard is the, most common channe,l to gathe,r use,r input. for this re,ason, python fe,ature,s a built-in input() function to re,cognize, ke,yboard e,ntry.
The, input() function
Python’s input() function colle,cts ke,yboard input inte,ractive,ly through an optional parame,te,r calle,d a prompt string.
The, program displays the, prompt string whe,ne,ve,r the, input function is calle,d. program e,xe,cution is suspe,nde,d until the, use,r e,nte,rs a re,sponse, to the, prompt.
To illustrate, this function, he,re,’s a simple, program that colle,cts name, and age, information from a use,r:
Name, = input(ple,ase, type, your name,:
)
Print(we,lcome,,
+ name, + !
)
Age, = input(how old are, you,
+ name, + ?
)
Print(thank you. you are,
+ name, + and you’re,
+ age, + ye,ars old.
)
Whe,n you run the, program and input marky for name, and 24 for age,, he,re,’s what the, scre,e,n would display on a pe,r-line, basis: ple,ase, e,ncode, your name,: marky
We,lcome,, marky!
How old are, you, marky? 24
Thank you. you are, marky, and you’re, 24 ye,ars old.
Output
Python’s built-in print() function is use,d to output data to the, scre,e,n, which is the, standard output de,vice,.
The, syntax for the, print command is: print(value,)
He,re, are, e,xample,s of its usage,:
>>> print(i am a programming stude,nt.
)
I am a programming stude,nt.
>>> print(25)
25
>>> print(4**3)
64
>>> xyz = 50
>>> print(‘the, value, of xyz is’ , xyz)
The, value, of xyz is 50
>>> animal = rhinoce,r0s
>>>print(animal)
Rhinoce,ros
Python allows printing of se,ve,ral value,s or variable,s which must be, se,parate,d by a comma inside, the, pare,nthe,se,s.
For e,xample,, the, following is a se,rie,s of assignme,nt state,me,nts:
A = une,mploye,d
B = 16 ye,ars old
C = re,side,nt
Assuming you want to print the, above, value,s as re,quire,me,nts, you can make, a print state,me,nt like,:
>>>print(job re,quire,me,nts:
, a, b, c)
The, output would be,:
Job re,quire,me,nts: une,mploye,d 16 ye,ars old re,side,nt
Output or string formatting
Formatting options allow use,rs to cre,ate, a more, re,adable, output. python provide,s the, str.format() me,thod to format string.
Using curly brace,s as place,holde,rs
Curly brace,s {} are, use,d as place,holde,rs for the, formatte,d value, spe,cifie,d in the, str.format() state,me,nt.
If the, position of the, argume,nts give,n in str.format() state,me,nt coincide,s with your de,sire,d output orde,r, you can use, e,mpty brace,s in your print state,me,nt: >>> x = 50; y = 100
>>> print(‘the, value, of x is {} and y is {}.’.format(x,y))
The, value, of x is 50 and y is 100.
If, howe,ve,r, you want the, give,n argume,nts to appe,ar in an orde,r that is diffe,re,nt from the,ir position in the, str.format() state,me,nt, you will ne,e,d to spe,cify the,ir inde,x inside, the, curly brace,s. the, first argume,nt has inde,x 0, the, se,cond argume,nt inde,x 1, and so on.
Using the, above, e,xample,, assuming you want the, value, of variable, b to appe,ar be,fore, the, value, of variable, a: >>> x = 50; y = 100
>>> print(‘the, value, of y is {1} and x is {0}.’.format(x,y))
The, value, of y is 100 and x is 50.
He,re, are, othe,r e,xample,s. this time,, the, value,s the,mse,lve,s are, give,n and not store,d in variable,s:
>>> print(‘i buy {}, {}, and {} coins.’.format(‘gold’, ‘silve,r’, ‘numismatic’))
I buy gold, silve,r, and numismatic coins.
>>> print(‘i colle,ct {2}, {0}, and {1} coins.’.format(‘gold’, ‘silve,r’, ‘numismatic’))
I colle,ct numismatic, gold, and silve,r coins.
String formatting with sprint() style,
Python 3 still supports string formatting using the, sprint() style, associate,d with the, c language,.
He,re,’s an e,xample,:
>>> print(‘catalog no.: %6d, price, pe,r unit: %7.2f’% (6450, 159.8745))
Catalog no.: 6450, price, pe,r unit: 159.87
Take, note, that the, first argume,nt, 6450, was formatte,d to print up to 6 digits (%6d). since, only 4 digits we,re, use,d, 2 le,ading space,s we,re, adde,d to the, output. the, se,cond argume,nt was formatte,d to print a floating-point numbe,r with 7 digits, including 2 de,cimal numbe,rs (%7.2f). since, only two de,cimal place,s we,re, provide,d, the, float was rounde,d off to two de,cimal place,s.
The, above, print state,me,nt use,s the, string modulo ope,rator to format the, string. you can translate, this state,me,nt to the, string format me,thod by using curly brace,s and positional argume,nts. he,re,’s how it would be, writte,n: >>> print(‘catalog no.: {0:6d}, price, pe,r pie,ce,: {1:7.2f}‘.format(6450, 159.8745))
Catalog no.: 6450, price, pe,r pie,ce,: 159.87
Python ope,rators he,lp us manipulate, the, value, of ope,rands in ope,rations. e,xample,:
10 * 34 = 340
In the, above, e,xample,, the, value,s 10 and 34 are, known as ope,rands, while, * is known as the, ope,rator. python supports diffe,re,nt type,s of ope,rators.
Arithme,tic ope,rators
The,se, are, the, ope,rators use,d for pe,rforming the, basic mathe,matical ope,rations. the,y include, multiplication (*), addition(+), subtraction (-), division (/), modulus (%) and othe,rs. e,xample,:
#!Usrbin/python3
N1 = 6
N2 = 5
N3 = 0
N3 = n1 + n2
Print(the, value, of sum is:
, n3)
N3 = n1 - n2
Print(the, re,sult of subtraction is:
, n3 )
N3 = n1 * n2
Print(the, re,sult of multiplication is:
, n3)
N3 = n1 / n2
Print (the, re,sult of division is:
, n3 )
N3 = n1 % n2
Print (the, re,mainde,r afte,r division is:
, n3)
N1 = 2
N2 = 3
N3 = n1**n2
Print (the, e,xpone,ntial value, is:
, n3)
N1 = 20
N2 = 4
N3 = n1//n2
Image00013.jpg Print (the, re,sult of floor division is:
, n3)
The, code, prints the, following whe,n e,xe,cute,d:
That is how the, arithme,tic ope,rations work in python. the, modulus ope,rator (%) re,turns the, re,mainde,r afte,r a division has be,e,n done,. in our case,, we, are, dividing 6 by 5, and the, re,mainde,r is 1.
Comparison ope,rators
The,se, ope,rators are, use,d for comparing the, value,s of ope,rands and ide,ntify the, re,lationship be,twe,e,n the,m. the,y include, the, e,qual to (==), not e,qual to (!=), le,ss than (<), gre,ate,r than (>), gre,ate,r than or e,qual to (>=) and le,ss than or e,qual to (<=).
E,xample,:
#!Usrbin/python3
N1 = 6
N2 = 5
If ( n1 == n2 ):
Print (the, two numbe,rs have, e,qual value,s
)
E,lse,:
Print (the, two numbe,rs are, not e,qual in value,
)
If ( n1 != n2 ):
Print (the, two numbe,rs are, not e,qual in value,
)
E,lse,:
Print (the, two numbe,rs are, e,qual in value,
)
If ( n1 < n2 ):
Print (n1 is le,ss than n2
)
E,lse,:
Print (n1 is not le,ss than n2
)
If (n1 > n2 ):
Print (n1 is gre,ate,r than n2
)
E,lse,:
Print (n1 is not gre,ate,r than n2
)
N1,n2=n2,n1 #the, value,s of n1 and n2 will be, swappe,d. n1=5, n2=6
If ( n1 <= n2 ):
The, code, will print the, following:
Image00014.jpgThe, value, of n1 is 6, while, that of n2 is 5. the, use, of the, e,qual to (==) ope,rator on the, two ope,rands will re,turn a false, as the, two ope,rands are, not e,qual. this will le,ad to the, e,xe,cution of the, e,lse,
part. the, ope,rator not e,qual to (!=) will re,turn a true, as the, value,s of the, two ope,rands are, not e,qual. the, only logic which might se,e,m comple,x in this case, is the, swapping of the, value,s. the, value, of n1, which is 6 be,come,s 5, while, that of n2 be,come,s 6. the, state,me,nts which are, be,low this swapping state,me,nt will the,n ope,rate, with the,se, two ne,w value,s.
Assignme,nt ope,rators
The,se, ope,rators, the, combination of the, assignme,nt ope,rator (=) with the, othe,r ope,rators. a good e,xample, of an assignme,nt ope,rator is +=
. the, e,xpre,ssion p+=q me,ans p=p + q
. the, e,xpre,ssion p/=q
me,ans that p=p / q
. the, assignme,nt ope,rators involve, combining the, assignme,nt ope,rator with the, re,st of the, othe,r ope,rators.
E,xample,:
#!Usrbin/python3
N1 = 6
N2 = 5
N3 = 0
N3 = n1 + n2
Print (the, value, of n3 is:
, n3)
N3 += n1
Print (the, value, of n3 is:
, n3 )
N3 *= n1
Print (the, value, of n3 is:
, n3 )
N3 /= n1
Print (the, value, of n3
, n3 )
N3 = 2
N3 %= n1
Print (the, value, of n3 is:
, n3)
N3 **= n1
Print (the, value, of n3 is:
, n3)
N3 //= n1
Image00015.jpg Print (the, value, of n3 is:
, n3)
The, code, will print the, following whe,n e,xe,cute,d:
The, state,me,nt n3 = n1 + n2
is ve,ry straight forward as we, are, just adding the, value, of n1 to that of n2. in the, e,xpre,ssion n3 += n1
, we, are, adding the, value, of n3 to that of n1 and the,n assign the, re,sult to n3. howe,ve,r, note, that in the, pre,vious state,me,nt, the, value, of n3 change,d to 11
afte,r adding n1 to n2. so we, have, 11+6, which give,s 17. afte,r that, the, ne,w value, of the, variable, n3 will be, 17. the, e,xpre,ssion n3 = n1
me,ans n3= n3 n1
. this will be, 17 * 6, and the, re,sult will be, 102. that is how the,se, ope,rators work in python!
Me,mbe,rship ope,rators
The,se, are, the, ope,rators that are, use,d for te,sting me,mbe,rship in a ce,rtain se,que,nce, of e,le,me,nts. the, se,que,nce, of e,le,me,nts can be, a string, a list, or a tuple,. the, two me,mbe,rship ope,rators include, in
and not in
.
The, in
ope,rator re,turns true, if the, value, you spe,cify is found in the, se,que,nce,. the, ope,rator not in
will e,valuate, to a true, if the, spe,cifie,d e,le,me,nt is not found in the, se,que,nce,.
E,xample,:
#!Usrbin/python3
N1 = 7
N2 = 21
Ls = [10, 20, 30, 40, 50 ]
If ( n1 in ls ):
Print (n1 was found in the, list
)
E,lse,:
Print (n1 was not found in the, list
)
If ( n2 not in ls ):
Print (n2 was not found in the, list
)
E,lse,:
Print (n2 was found in the, list
)
N3=n2/n1
If ( n3 in ls ):
Print (n1 was found in the, list
)
E,lse,:
Image00016.jpgPrint (n1 was not found in the, list
)
The, code, will print the, following once, e,xe,cute,d:
The, value, of num1 is 7. this is not part of the, list; it is why the, use, of the, in
ope,rator re,turns a false,. this cause,s the, e,lse,
part to be, e,xe,cute,d. the, value, of n2 is 21. this is not in the, list. this e,xpre,ssion re,turns a true,, and the, first part be,low the, e,xpre,ssion is e,xe,cute,d. 21 divide, by 7 is 3. this value, is not in the, list. the, use, of the, last in
ope,rator e,valuate,s to a false,, and that is why the, e,lse,
part be,low it is e,xe,cute,d.
Ide,ntity ope,rators
The,se, ope,rators are, use,d to compare, the, value,s of two me,mory locations. python has a me,thod name,d id()
that re,turns the, unique, ide,ntifie,r of the, obje,ct. python has two ide,ntity ope,rators:
Is - this ope,rator e,valuate,s to a true, in case, the, variable,s use,d on e,ithe,r side, of the, ope,rator are, pointing to a similar obje,ct. it e,valuate,s to false, othe,rwise,.
Is not - this ope,rator e,valuate,s to a false, if the, variable,s on e,ithe,r side, of the, ope,rator are, pointing to a similar obje,ct, and true, othe,rwise,.
E,xample,:
#!Usrbin/python3
N1 = 45
N2 = 45
Print (‘the, initial value,s are,’,’n1=‘,n1,’:‘,id(n1), ‘n2=‘,n2,’:‘,id(n2))
If ( n1 is n2 ):
Print (1. n1 and n2 share, an ide,ntity
)
E,lse,:
Print (2. n1 and n2 do not share, ide,ntity
)
If ( id(n1) == id(n2) ):
Print (3. n1 and n2 share, an ide,ntity
)
E,lse,:
Print (4. n1 and n2 do not share, ide,ntity
)
N2 = 100
Print (‘the, variable, value,s are,’,’n1=‘,n1,’:‘,id(n1), ‘n2=‘,n2,’:‘,id(n2))
If ( n1 is not n2 ):
Print (5. n1 and n2 do not share, ide,ntity
)
E,lse,:
Image00017.jpgPrint (6. n1 and n2 share, ide,ntity
)
The, code, will print the, following once, e,xe,cute,d:
Note, that i have, numbe,re,d some, of the, print state,me,nts so that it may be, e,asy to diffe,re,ntiate, the,m. in the, first instance,, the, value,s of variable,s n1 and n2 are, e,qual. the, first state,me,nt of the, output shows the, re,spe,ctive, value,s for the, variable,s toge,the,r with the,ir unique, ide,ntifie,r. note, that the, ide,ntifie,r has be,e,n obtaine,d by use, if the, id() python me,thod, and the, name, of the, variable, has be,e,n passe,d inside, the, function as the, argume,nt. the, e,xpre,ssion if ( n1 is n2 ):
will e,valuate, to a true, since, the, value,s of the, two variable,s are, e,qual, or the,y are, pointing to a similar obje,ct. this is why the, print state,me,nt labe,le,d 1 was e,xe,cute,d!
You must also have, notice,d that the, unique, ide,ntifie,rs of the, two variable,s are, e,qual. in the, e,xpre,ssion if ( id(n1) == id(n2) ):
, we, are, te,sting whe,the,r the, value,s of the, ide,ntifie,rs for the, two variable,s are, the, same,. this e,valuate,s to a true,; he,nce, the, print state,me,nt labe,le,d 3 has be,e,n e,xe,cute,d!
The, e,xpre,ssion n2 = 100
change,s the, value, of variable, n2 from 45 to 100. at this point, the, value,s of the, two variable,s will not be, e,qual. this is be,cause, n1 has a value, of 45, while, n2 has a value, of 100. this is cle,arly in the, ne,xt print state,me,nt, which shows the, value,s of the, variable,s toge,the,r with the,ir corre,sponding ids. you must also have, notice,d that the, ids of the, two variable,s are, not e,qual at this point.
The, e,xpre,ssion if ( n1 is not n2 ):
e,valuate,s to a true,; he,nce, the, print state,me,nt labe,le,d 5 was e,xe,cute,d. if we, te,st to che,ck whe,the,r the, value,s of the, ids for the, two variable,s are, e,qual, you will notice, that the,y are, not e,qual.
Chapte,r 3: loops and function - variable, function in python
Loops
The, ability to make, de,cisions is a critical compone,nt of most compute,r programs. anothe,r is the, ability to re,pe,at or loop the, program through a spe,cific se,t of tasks.
All programs, othe,r than those, that pe,rform a spe,cific task and e,xit, contain at le,ast one, loop. typically, this is the, main loop, whe,re, the, program continually loops, waiting for use,r or obje,ct input for it to act upon.